开发者

Checking Multiple Checkboxes in Android

开发者 https://www.devze.com 2023-02-04 13:12 出处:网络
CheckBox checkOne = (CheckBox) findViewById(R.id.checkOne); checkOne.setSelected(true); CheckBox checkTwo = (CheckBox) findViewById(R.id.checkTwo);
CheckBox checkOne = (CheckBox) findViewById(R.id.checkOne);
checkOne.setSelected(true);

CheckBox checkTwo = (CheckBox) findViewById(R.id.checkTwo);
checkTwo.setSelected(true);

CheckBox checkThree = (CheckBox) findViewById(R.id.checkThree);
checkThree.setSelected(true);

I am using the above code to check or uncheck multiple check boxes.

My question is, is there any way I can get all check boxes at once? (I don't wanna use arrays) For example, my check boxes are开发者_如何学Go in a LinearLayout, so can I get all the check boxes as the child of LinearLayout?


If the parent LinearLayout contains only the checkboxes, you can do this:

//ll is the LinearLayout holding the children
for(int i = 0; i < ll.getChildCount(); i++) {
    ((CheckBox)ll.getChildAt(i)).setChecked(true);
}

if you have more views in the LinearLayout you could add a check like this:

for(int i = 0; i < ll.getChildCount(); i++) {
    View v = ll.getChildAt(i);
    if(v instanceof CheckBox) {
        ((CheckBox)v).setChecked(true);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消