How to set radio button checked as default in radiogroup?

前端 未结 8 1523
孤城傲影
孤城傲影 2020-12-23 09:01

I have created RadioGroup and RadioButton dynamically as following:

RadioGroup radioGroup = new RadioGroup(context);
                      


        
相关标签:
8条回答
  • 2020-12-23 09:09

    It's a bug of RadioGroup

    RadioButton radioBtn2 = new RadioButton(context);
    

    radioBtn2 without viewId, and generateViewId is in onChildViewAdded()

    public void onChildViewAdded(View parent, View child) {
        if (parent == RadioGroup.this && child instanceof RadioButton) {
            int id = child.getId();
            // generates an id if it's missing
            if (id == View.NO_ID) {
                id = View.generateViewId();
                child.setId(id);
            }
            ((RadioButton) child).setOnCheckedChangeWidgetListener(
                    mChildOnCheckedChangeListener);
        }
    
        if (mOnHierarchyChangeListener != null) {
            mOnHierarchyChangeListener.onChildViewAdded(parent, child);
        }
    }
    

    so, first radioGroup.addView(radioBtn2), then radioBtn2.setChecked(true);

    Like this:

    RadioGroup radioGroup = new RadioGroup(context);
    RadioButton radioBtn1 = new RadioButton(context);
    RadioButton radioBtn2 = new RadioButton(context);
    RadioButton radioBtn3 = new RadioButton(context);
    
    radioBtn1.setText("Less");
    radioBtn2.setText("Normal");
    radioBtn3.setText("More");
    
    radioGroup.addView(radioBtn1);
    radioGroup.addView(radioBtn2);
    radioGroup.addView(radioBtn3);
    
    radioBtn2.setChecked(true);
    
    0 讨论(0)
  • 2020-12-23 09:10

    In the XML file set the android:checkedButton field in your RadioGroup, with the id of your default RadioButton:

    <RadioGroup
        ....
        android:checkedButton="@+id/button_1">
    
        <RadioButton
            android:id="@+id/button_1"
            ...../>
    
        <RadioButton
            android:id="@+id/button_2"
            ...../>
    
        <RadioButton
            android:id="@+id/button_3"
            ...../>
    </RadioGroup>
    
    0 讨论(0)
  • 2020-12-23 09:14

    There was same problem in my Colleague's code. This sounds as your Radio Group is not properly set with your Radio Buttons. This is the reason you can multi-select the radio buttons. I tried many things, finally i did a trick which is wrong actually, but works fine.

    for ( int i = 0 ; i < myCount ; i++ )
    {
        if ( i != k )
        {
            System.out.println ( "i = " + i );
            radio1[i].setChecked(false);
        }
    }
    

    Here I set one for loop, which checks for the available radio buttons and de-selects every one except the new clicked one. try it.

    0 讨论(0)
  • 2020-12-23 09:15

    In case for xml attribute its android:checkedButton which takes the id of the RadioButton to be checked.

    <RadioGroup
    ...
    ...
    android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked"
    ... >....</RadioGroup>
    
    0 讨论(0)
  • 2020-12-23 09:17

    Add android:checked = "true" in your activity.xml

    0 讨论(0)
  • 2020-12-23 09:18

    you should check the radiobutton in the radiogroup like this:

    radiogroup.check(IdOfYourButton)

    Of course you first have to set an Id to your radiobuttons

    EDIT: i forgot, radioButton.getId() works as well, thx Ramesh

    EDIT2:

    android:checkedButton="@+id/my_radiobtn"
    

    works in radiogroup xml

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