如何使用android中另一个复选框的标记值取消选中复选框
我有许多复选框动态创建 现在我只想选择其中任何一个,而不是多个。当我选择第二个时,第一个应该取消选择 我不能使用广播组。有什么帮助吗 这是我的密码。。我正在动态创建复选框 私有void createRadioButton(){ cb=新复选框(fContext)如何使用android中另一个复选框的标记值取消选中复选框,android,checkbox,Android,Checkbox,我有许多复选框动态创建 现在我只想选择其中任何一个,而不是多个。当我选择第二个时,第一个应该取消选择 我不能使用广播组。有什么帮助吗 这是我的密码。。我正在动态创建复选框 私有void createRadioButton(){ cb=新复选框(fContext) 使用单选复选框的一个非常简单的示例。试试这个 CheckBox myCheckBox[]=newcheckbox[noofCheckBox]; 对于(int i=0;i您可以保存上次选中的复选框,并在选中其他复选框时取消选中它 在公共
使用单选复选框的一个非常简单的示例。
试试这个
CheckBox myCheckBox[]=newcheckbox[noofCheckBox];
对于(int i=0;i您可以保存上次选中的复选框,并在选中其他复选框时取消选中它
在公共区域中定义此项
HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();
你尝试过什么?发布你的代码,这样我们就可以知道你的目标是什么。@Kris你想使用单选按钮,而不是复选框。你是指单选组。事实上,我正在为一个跨平台应用程序编写代码。我试过了,但它不能正常工作。然后我决定使用复选框。链接显示不同的复选框。用这些复选框可以轻松地选中和取消选中在这里,我创建了50个复选框,例如带有不同标记的复选框。您正在使用for循环创建?我编辑了答案。因为当选中时,然后取消选中同一复选框不起作用。现在它起作用了。
CheckBox myCheckBox[] = new CheckBox[noofCheckBox];
for (int i=0; i<noofCheckBox; i++) {
myCheckBox[i] = new myCheckBox(this);
myCheckBox[i].setId(i+1);
myCheckBox[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if(myCheckBox[i].getId() == getPosition){
myCheckBox[i].setChecked(true);
}else{
myCheckBox[i].setChecked(false)
}
}
});
}
HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//int getPosition = (Integer) buttonView.getTag();
if(isChecked){
if(hash.size()>0){
hash.get("1").setChecked(false);
}
hash.put("1", buttonView);
}else{
hash.clear();
}
Log.i("comVisa","getPos =="+getPosition);
}
});