如何取消选中已选中的 Android 复选框 [英] How to uncheck a checked Android Checkbox

查看:19
本文介绍了如何取消选中已选中的 Android 复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Android 应用中有两个单选按钮和 5 个复选框.还有一个保存按钮.当用户单击保存按钮时,我需要取消选中用户选中的复选框.我已尝试使用以下代码.但它不起作用.

I have two radio buttons and 5 checkboxes in my android app. and also a save button. When the user clicks save button I need to uncheck the checkboxes checked by the user. I have tried with the following code.But it is not working.

if (chkOthers.isChecked()) 
    chkOthers.setChecked(false);
    chkOthers.setSelected(false);
}

推荐答案

只需使用 chk1.toggle() onClick the button 取消选中的.

Just use chk1.toggle() onClick of the button to uncheck the checked ones.

public class TestCheckBoxActivity extends Activity {
  /** Called when the activity is first created. */
     CheckBox chk1, chk2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        chk1 = (CheckBox)findViewById(R.id.checkBox1);
        chk2 = (CheckBox)findViewById(R.id.checkBox2);

        Button btn = (Button)findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(chk1.isChecked()){
                chk1.toggle();
            }

            if(chk2.isChecked()){
                chk2.toggle();
            }

        }
    });
       }
}

这篇关于如何取消选中已选中的 Android 复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆