我的程序在一页上有 50 多个复选框。 我需要检查选择了哪些框。
我知道我可以做这样的事情:
CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
if (cb1.isChecked){
//get Checkbox name
}
if (cb2.isChecked){
//get Checkbox name
}
但是,如果我必须对超过 50 个复选框执行此操作,则需要一些时间。 他们是否可以更快地检查选择了哪个?像这样的东西:
int i;
for (i = 0; i<checkBox.length; i++){
CheckBox cb+i = (CheckBox) findViewById (R.id.checkBox+i);
if (cb+i.isChecked){
//get Checkbox name
}
}
或许也可以这么说:您可以选择 1 个以上的复选框。 我希望你明白我的意思。
已经谢谢了, 大流量
最佳答案
另一种解决方法可能是:
假设您在LinearLayout
中添加了CheckBox
,您可以在Java中获取LinearLayout
引用并获取子View,如下所示:
LinearLayout layout= (LinearLayout)findViewById(R.id.checkbox_container);
ArrayList values = new ArrayList();
for(int i=0; i<layout.getChildCount(); i++)
{
View v = layout.getChildAt(i);
if(v instanceof CheckBox)
{
CheckBox cb = (CheckBox)v;
if(cb.isChecked())
values.add(cb.getText().toString());
}
}
关于java - 检查哪个复选框被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10444485/