设置默认选中的单选按钮

12

我正在动态创建一个包含多个单选按钮的RadioGroup,需要默认选中其中一个单选按钮。

我使用了radioButton.setChecked(true)radioButton.toggle();,成功实现了默认选中一个单选按钮。

但是问题是,当我在运行时选择另一个单选按钮时,第一个单选按钮仍然保持选中状态,因此在单选组中会有两个选中的单选按钮。

请问是否有人遇到过这个问题并知道如何解决?

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;
    }

你确定你将单选按钮添加到了正确的单选组中吗? - ridvanzoro
9个回答

12

我知道我来晚了,但对于像我一样在寻找答案的人来说,这可能会有用。

当您将单选按钮添加到 RadioGroup 时,必须在将其添加到父级后设置按钮为已选中,如下所示:

RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);
如果在将视图添加到父级之前先勾选它,则无法取消勾选。

4
您可以简单地这样做:
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>

2

首次加载屏幕时,在Getview方法中检查In的位置,然后应用条件:

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

1
如果您只使用一个单选框来进行选中和取消选中操作,或许应该使用复选框或切换按钮。

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

向下滚动并查看复选框和切换按钮。

使用单选按钮时,通常有多个选项可供选择。例如:简单、中等、困难。

替换

radio.toggle();

使用

radio.setChecked(true);

我尝试了两种方法,它们都会勾选按钮,但如果我勾选另一个按钮,该按钮仍然保持勾选状态。 - thehindutimes
你是将它们添加到单个RadioGroup中吗? - SilentKiller

1
在你的代码中,只需要将 checked = "true" 设为真即可。

0

你应该在单选按钮组中检查它...

radiogroup.check(IdOfYourButton)

0

原来是因为我在设置OnCheckedChangeListener()后立即调用了radio group上的checked()方法,导致它立即被调用并出现某些原因的NPE异常。

将checked()方法调用移动到OnCheckedChangeListener()之前,现在它可以正常工作了。

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;
    }

0
使用 clearCheck() 函数清除已选中的按钮。希望这能解决您的问题。

0
RadioGroup中添加android:checkedButton="@+id/storeRB"Xml
 <RadioGroup
                android:id="@+id/storeTypeRG"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checkedButton="@+id/storeRB"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="@+id/text2"
                app:layout_constraintStart_toEndOf="@+id/text2"
                app:layout_constraintTop_toTopOf="@+id/text2">

                <RadioButton
                    android:id="@+id/storeRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="24dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/store_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/text2"
                    app:layout_constraintTop_toTopOf="@+id/text2" />

                <RadioButton
                    android:id="@+id/hyperRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/hyper_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/storeRB"
                    app:layout_constraintTop_toTopOf="@+id/text2" />

                <RadioButton
                    android:id="@+id/restaurantRB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:button="@drawable/radio_button_bg"
                    android:paddingStart="8dp"
                    android:text="@string/restaurant_str"
                    app:layout_constraintBottom_toBottomOf="@+id/text2"
                    app:layout_constraintStart_toEndOf="@+id/hyperRB"
                    app:layout_constraintTop_toTopOf="@+id/text2" />
            </RadioGroup>

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接