Set selected index of an Android RadioGroup

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

    Siavash's answer is correct:

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

    But be aware that a radioGroup can contain views other than radioButtons -- like this example that includes a faint line under each choice.

    
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
    
    

    In this case using an index of 1, for example, would generate an error. The item at index 1 is the first separator line -- not a radioButton. The radioButtons in this example are at indexes 0, 2, 4, 6.

提交回复
热议问题