Set selected index of an Android RadioGroup

前端 未结 7 1064
予麋鹿
予麋鹿 2021-02-01 11:52

Is there a way to set the selected index of a RadioGroup in android, other than looping through the child radiobuttons and selecting checking the radio button at the selected in

7条回答
  •  情话喂你
    2021-02-01 12:15

    If your radio group is defined in a layout xml file, each button can be assigned an id. Then you just check a button like this

    radioGroup.check(R.id.myButtonId);
    

    If you created your radio group programmatically (I'm not even sure how you do this...), you might want to consider creating a special layout xml file just for the radio group so that you can assign R.id.* ids to the buttons.

    Please see the answer below if you are, in fact, looking to set the radio button group by index, see the answer below.

    ((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
    

提交回复
热议问题