Is there any way of getting an array (or a collection) of the RadioButton
s in an Android RadioGroup
? I would like to add individual listeners to radio buttons but I don't see any obvious way of iterating over them.
问问题
23869 次
2 回答
62
这应该可以解决问题:
int count = radioGroup.getChildCount();
ArrayList<RadioButton> listOfRadioButtons = new ArrayList<RadioButton>();
for (int i=0;i<count;i++) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
listOfRadioButtons.add((RadioButton)o);
}
}
Log.d(TAG,"you have "+listOfRadioButtons.size()+" radio buttons");
于 2011-02-23T19:59:43.347 回答
1
为什么需要查询radioGroup?为什么你不能直接在 RadioButton 上设置你的监听器,你必须能够获得 radioButtons,因为你是将它们添加到 RadioGroup 的人。
无论如何,RadioGroup 只是一种特殊类型的 LinearLayout,它的所有子元素都是您添加的 RadioButton。您可以遍历所有子视图以访问 RadioButtons。
于 2011-02-23T20:08:21.930 回答