Set selected index of an Android RadioGroup

前端 未结 7 1049
予麋鹿
予麋鹿 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:24

    Question said "set selected INDEX", here's how to set it by index:

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

    ........

    Additional info: It seems that Google wants you to use id instead of index, because using id is more bug proof. For example, if you have another UI element in your RadioGroup, or if another developer re-orders the RadioButtons, the indices might change and not be what you expected. But if you're the only developer, this is totally fine.

    0 讨论(0)
提交回复
热议问题