在Android中获取RadioGroup中的单选按钮数组

在Android中获取RadioGroup中的单选按钮数组,android,arrays,radio-button,iteration,radio-group,Android,Arrays,Radio Button,Iteration,Radio Group,有没有办法在AndroidRadioGroup中获取RadioButtons的数组(或集合)?我想在单选按钮中添加单独的侦听器,但我看不到任何明显的迭代方式。这应该可以做到: int count = radioGroup.getChildCount(); ArrayList<RadioButton> listOfRadioButtons = new ArrayList<RadioButton>(); for (int i=0

有没有办法在Android
RadioGroup
中获取
RadioButton
s的数组(或集合)?我想在单选按钮中添加单独的侦听器,但我看不到任何明显的迭代方式。

这应该可以做到:

        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");
int count=radioGroup.getChildCount();
ArrayList listOfRadioButtons=新建ArrayList();

对于(int i=0;i为什么您需要查询radioGroup?为什么您不能直接在RadioButton上设置您的侦听器,您必须能够获得RadioButton,因为您是将其添加到RadioButton的人


无论如何,RadioGroup只是一种特殊类型的LinearLayout,它的所有子项都是您添加的单选按钮。您可以在所有子视图中循环访问单选按钮。

我在XML布局中创建了
RadioGroup
,因此所有的
RadioButton
都已经是其中的一部分。我从未在代码中手动添加它们。谢谢你的回答嘿,我也创建了一个单选按钮数组,但我想取消选中全部,然后只从该数组列表中选中一个。我怎么做??