Get the array of RadioButtons in a RadioGroup in Android

后端 未结 2 541
谎友^
谎友^ 2020-12-05 17:30

Is there any way of getting an array (or a collection) of the RadioButtons in an Android RadioGroup? I would like to add individual listeners to ra

相关标签:
2条回答
  • 2020-12-05 18:18

    Why do you need to query the radioGroup? Why cant you directly set your listeners on the RadioButton, you must be able to get a hold of the radioButtons since you are the one who is adding them to the RadioGroup.

    Regardless, RadioGroup is simply a special type of LinearLayout, all its children are RadioButtons that you have added. You can loop through all the child views to access the RadioButtons.

    0 讨论(0)
  • 2020-12-05 18:24

    this should do the trick:

            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");
    
    0 讨论(0)
提交回复
热议问题