简体   繁体   中英

Set default checked Radio Button

I'm creating a RadioGroup with RadioButton s dynamically and need to have one radio button checked by default.

I've done this by using both radioButton.setChecked(true) and radioButton.toggle();

The problem I have is that when I at runtime select another radio button the first one stays checked so I end up with two checked radio buttons in the radio group.

Has anyone had this problem and know how to solve it?

private RadioButton addRadioButton(String type, String price){
        RadioButton radio = new RadioButton(Order.this);
        radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
        radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
        radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        radio.setTag(price);

        if(type.toLowerCase().equals("m"))
            radio.toggle();

        return radio;
    }

I know I'm late but for those who search for the answer like me this can be useful.

When you add RadioButton to a RadioGroup, you must set the button checked after adding it to the parent like:

RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);

If the view is checked before adding it to the parent, it will be impossible to uncheck it.

You can do this simply:

JAVA:

    radiogroup =(RadioGroup)findViewById(R.id.radiogroup);
    radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
    radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2);

    if(your first assessment){
        radiobutton1.setChecked(true);
    }else{
        radiobutton2.setChecked(true);
    }

XML:

   <RadioGroup
        android:id="@+id/radiogroup"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
        android:id="@+id/radiobutton1"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </RadioGroup>

Simple, at first time load the screen check the position of In Getview method and then apply condition:

if (position == 0) {
  holder.act_delivery_rbtn.setChecked(true);
}

If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

Scroll down and see checkbox and toggle button.

When using radios you usually have more than one and choose between them. Like easy, medium, hard.

Replace

radio.toggle();

with

radio.setChecked(true);

只需在代码中设置checked =“true”即可

你应该在电台组中查看...

radiogroup.check(IdOfYourButton)

使用clearCheck()函数清除已经检查过的按钮。我希望这能解决你的问题。

Turned out to be because I called the checked() method on the radio group straight after setting the OnCheckedChangeListener() causing it to be called immediately and throw a NPE for some reason.

Moved the checked() method call to just before the OnCheckedChangeListener() and it works now.

radios.check(2);
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() {   
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                }
            });

private RadioButton addRadioButton(String type, String price){
        RadioButton radio = new RadioButton(Order.this);
        radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
        radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
        radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        radio.setTag(price);

        if(type.toLowerCase().equals("m"))
            radio.setId(2);

        return radio;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM