本文介绍了如何处理检查和取消android中动态创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用循环复选框,我想验证它。就像我刚才想只有3从复选框,当我在$ 4一个p $ PSS检查它应该显示一个警告,并取消它。
,我能够得到警报,当我preSS的4the之一,但它不是取消选中。
任何人都面临这样的问题,你是如何解决的?
解决方案
INT I;
对于(I = 0; I&小于20;我++){
复选框CH =新的复选框(本);
ch.setTag(Integer.valueOf(i)段);
ch.setText(复选框+ I);
ch.setChecked(假);
ch.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
如果(器isChecked){
numChecked ++;
}其他{
numChecked--;
} 如果(numChecked == 4){
buttonView.setChecked(假);
numChecked--;
//第四个选择,显示你的对话
}
}
});
}
您还需要一个全局变量调用numChecked:
INT numChecked = 0;
您还需要在循环的结束复选框添加到您的布局添加.addView(CH)。
i have created check boxes using loop and i want to validate it. Like i just want to check only 3 from the check boxes , when i press on the 4th one it should show an alert and uncheck it.
And i am able to get the alert when i press the 4the one but it is not unchecking.
anybody faced such issue and how did you solve it ?
解决方案
int i;
for (i = 0; i < 20; i++) {
CheckBox ch = new CheckBox(this);
ch.setTag(Integer.valueOf(i));
ch.setText("CheckBox " + i);
ch.setChecked(false);
ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
numChecked++;
} else {
numChecked--;
}
if (numChecked == 4) {
buttonView.setChecked(false);
numChecked--;
// fourth one selected, show your dialog
}
}
});
}
You will also need a global variable call numChecked:
int numChecked = 0;
You will also need to add a .addView(ch) in the loop's end to add the CheckBoxes to your layout.
这篇关于如何处理检查和取消android中动态创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!