我需要取消选中复选框当一个特定的复选框被选中 [英] I need to uncheck a checkbox when a particular checkbox is checked

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

问题描述

我是新手...我有两个复选框例如checkbox1和checkbox2。我想checkbox2被选中,当用户签checkbox1,反之亦然。 plz帮助我如何code此功能。

I am a newbie... I have two checkboxes for example checkbox1 and checkbox2. I want checkbox2 to be unchecked when user checks checkbox1 and vice versa. plz help me how to code for this functionality.

推荐答案

我想扩大HenrikS的回答。如果你仍然想使用复选框对于这一点,你可以在两个复选框设置onItemClickListener,需要取消选择其他的onItemClick()方法。一个例子是这样的: -

I want to expand HenrikS' answer. If you still want to use checkboxes for this, you can set an onItemClickListener on both the checkboxes and need to unselect other in the onItemClick() Method. An example would be like this:-

CheckBox cb1,cb2;
//Considering you can initialize the above variables
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb2.setChecked(false);
    }
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb1.setChecked(false);
    }
});

由于这样的麻烦,你应该使用单选按钮。

Due to this hassle, you should use radio buttons.

这篇关于我需要取消选中复选框当一个特定的复选框被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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