int count = listView.getChildCount();
for (int i = 0; i < count; i++) {
View child = list.getChildAt(i);
//check that child..
}
我想使用以下代码查看是否选中了checkBox的总数。就像我有3个复选框一样,我想要的东西等同于:
if(!list.getChildAt(0) && !list.getChildAt(1) && !list.getChildAt(2)){
// do something with all unchecked checkbox
}
我如何像这样循环浏览,因为我不确定复选框中的内容数量。
最佳答案
只需修改if语句即可返回复选框的状态。
int count = listView.getChildCount();
boolean allUnchecked = true;
for (int i = 0; i < count; i++) {
Object child = (Object) listView.getChildAt(i);
if (child instanceof CheckBox) {
CheckBox checkBoxChild = (CheckBox) child;
if (checkBoxChild.isChecked()) {
allUnchecked = false; //one is checked, sufficient to say that not all is unchecked
break; //get out the for loop
}
}
}
如果未选中所有复选框,则
allUnchecked
将为true,否则为false我不是Android开发人员,也找不到
getChildAt
的文档,所以我不知道它返回的内容。如果它是对象,则可以省略强制类型转换。检查
null
返回的getChildAt
也很好。附言:这不是一个好代码,您可以像伪代码一样使用它来了解如何实现是否进行检查的逻辑,获取CheckBoxes列表是您的任务:)