在 Android 中检查多个复选框 [英] Checking Multiple Checkboxes in Android

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

问题描述

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.

我的问题是,有什么办法可以一次获得所有复选框?(我不想使用数组)比如我的复选框在一个LinearLayout里面,那么我能不能把所有的复选框都作为LinearLayout的孩子?

推荐答案

如果父 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);
}

如果您在 LinearLayout 中有更多视图,您可以添加如下检查:

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);
    }
}

这篇关于在 Android 中检查多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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