如何在RadioGroup中设置一次只能选中一个RadioButton

How to set only one RadioButton Can be selected at the time in RadioGroup

我在radiogroup中创建了一个单选按钮,但是当我尝试运行应用程序所有的单选按钮都可以一直被选中,如何设置一次只能选择一个单选按钮?

我正在使用 片段

RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // find which radio button is selected
                if(checkedId == R.id.Abdominal) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: A",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Arm) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: B",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Back){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: C",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Chest){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: D",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Leg){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: E",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Shoulder){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: F",
                            Toast.LENGTH_SHORT).show();
                }
            }

        });

这里是我的 xml RG 和 RB 代码

<RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/RGroup">

                    <TableRow android:weightSum="1">
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Abdominal"
                        android:id="@+id/Abdominal"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Arm"
                        android:id="@+id/Arm"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Back"
                        android:id="@+id/Back" />
                        </TableRow>
                    <TableRow>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Chest"
                            android:id="@+id/Chest"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Leg"
                            android:id="@+id/Leg"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Shoulder"
                            android:id="@+id/Shoulder"/>
                    </TableRow>
                </RadioGroup>

已编辑 1:回答: 如果你不希望单选按钮可以一次选中,那么不要使用 Tablerow

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:id="@+id/radioGroup">
        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="wrap_content"
            android:text="Debit"
            android:id="@+id/rDebit"
            android:checked="false"
             />

        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="wrap_content"
            android:text="Credit"
            android:id="@+id/rCredit"
            android:checked="false" />

    </RadioGroup>

并在 java 文件中

 RadioGroup radioGroup;



radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

什么时候做某事

 if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}

由于 RadioGroup 中的 TableRow,它无法正常工作。由于它们之间有 TableRow,所以所有 RadioButton 都没有组合在一起。

RadioButton 应该是 RadioGroup 的直接子级,否则分组不起作用。

只需像这样更改您的代码即可:

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RGroup">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Abdominal"
                android:id="@+id/Abdominal"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Arm"
                android:id="@+id/Arm"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:id="@+id/Back" />                                        

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chest"
                android:id="@+id/Chest"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leg"
                android:id="@+id/Leg"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Shoulder"
                android:id="@+id/Shoulder"/>

        </RadioGroup>

希望这对您有所帮助。 :)

简单的方法。单击单选按钮。按照下面的代码做。

public void clearRadioChecked() {
    rdopa.setChecked(false);
    rdopb.setChecked(false);
    rdopc.setChecked(false);
    rdopd.setChecked(false);
}

如果你想 select rdopa 然后点击 rdopa 做如下。

clearRadioChecked()
rdopa.setChecked(true);

您可以在 RadioGroup 上使用 android:checkedButton 属性,提供您想要的 RadioButtonid最初选中并选择另一个 RadioButton 将清除之前的选择。

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="50dp"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/rbNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No" />
</RadioGroup>

您可以创建自己的 RadioGroup,它能够找到嵌套在您的组中的所有 RadioButtons,无论直接子节点还是不是。在我的 NestedRadioGroup 的代码下方。在你的 xml 文件中使用这个而不是你的 RadioGroup 应该可以解决问题。

public class NestedRadioGroup extends LinearLayout {
    private SparseArray<RadioButton> radioButtons;
    private int checkedId;

    public NestedRadioGroup(Context context) {
        this(context, null);
    }

    public NestedRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        radioButtons = new SparseArray<>();
        checkedId = -1;
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        findRadioButtons(child);
    }

    private void findRadioButtons(View child) {
        if (child instanceof RadioButton) {
            RadioButton newButton = (RadioButton) child;
            newButton.setId(radioButtons.size());
            newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (isChecked) {
                    if (checkedId != -1) {
                        radioButtons.get(checkedId).setChecked(false);
                    }
                    radioButtons.get(buttonView.getId()).setChecked(true);
                    checkedId = buttonView.getId();
                }
            });
            radioButtons.put(newButton.getId(), newButton);
        } else if (child instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) child;

            for (int i = 0; i < group.getChildCount(); i++) {
                this.findRadioButtons(group.getChildAt(i));
            }
        }
    }
}

有什么让代码更简洁的想法吗?

希望对你有用:)

我遇到了同样的问题,发现哪里出错了,我描述如下:

  1. 如果您在 RadioGroup 中使用 RadioButton,那么 RadioButtons 应该是 Direct Child of RadioGroup

  2. 检查第一个条件,如果正确则将id交给RatioButton和运行项目,它会自动解决问题。

我注意到如果不将 id 设置为单选按钮,单选将不起作用。

             <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/expenseRadio"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/expense" />

                <RadioButton
                    android:id="@+id/incomeRadio"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/income" />
            </RadioGroup>

我们可以在 Kotlin 中使用以下方法将 LinearLayout RelativeLayoutConstraintLayout GridLayout 等任何布局用作 RadioGroup

对于以下示例,我使用 ConstraintLayoutRadioButton 获取固定宽度和高度。

layout.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/rbYellow"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/space_2x"
        android:layout_marginEnd="@dimen/space"
        android:gravity="center"
        android:text="Yellow"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@+id/rbOrange"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioButton
        android:id="@+id/rbOrange"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/space"
        android:layout_marginEnd="@dimen/space"
        android:gravity="center"
        android:text="Orange"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@id/rbYellow"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@+id/rbRed"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/rbYellow"
        app:layout_constraintTop_toTopOf="@id/rbYellow" />

    <RadioButton
        android:id="@+id/rbRed"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/space"
        android:layout_marginEnd="@dimen/space"
        android:gravity="center"
        android:text="Red"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@id/rbYellow"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@+id/rbGreen"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/rbOrange"
        app:layout_constraintTop_toTopOf="@id/rbYellow" />

    <RadioButton
        android:id="@+id/rbGreen"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/space"
        android:layout_marginEnd="@dimen/space_2x"
        android:gravity="center"
        android:text="Green"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@id/rbYellow"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/rbRed"
        app:layout_constraintTop_toTopOf="@id/rbYellow" />
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin 代码

radioGroup.children.forEach { aBtn ->
    (aBtn as RadioButton).setOnCheckedChangeListener { button, isChecked ->
        if(isChecked) {
            radioGroup.children.forEach { bBtn ->
                if(bBtn.id != button.id) {
                    (bBtn as RadioButton).isChecked = false
                }
            }
        }
    }
}