如何只设置一个RadioButton可以在RadioGroup中选择

时间:2016-05-19 03:39:23

标签: android android-radiogroup android-radiobutton

我在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();
                }
            }

        });

这里是我的RG和RB的xml代码

<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

7 个答案:

答案 0 :(得分:20)

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

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>

希望这会有所帮助。 :)

答案 1 :(得分:5)

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

答案 2 :(得分:4)

简单的方法。点击单选按钮。按照以下方式编写代码。

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

如果你想选择rdopa,那么点击rdopa,如下所示。

clearRadioChecked()
rdopa.setChecked(true);

答案 3 :(得分:0)

您可以使用 RadioGroup 上的android:checkedButton属性,提供您想要首先检查的 RadioButton id,然后选择另一个 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>

答案 4 :(得分:0)

您可以创建自己的 RadioGroup ,该功能可以找到嵌套在您的组中的所有 RadioButton ,而不管是否有直接子级。在我的 NestedRadioGroup 的代码下方。使用此方法而不是在XML文件中使用 RadioGroup 即可解决问题。

result.isSuccesful==true

有什么想法可以使代码更简洁?

我希望它对您有用:)

答案 5 :(得分:0)

我现在正面临着同样的问题,发现我在哪里犯了错误,我将在下面进行描述:

  1. 如果您在RadioGroup中使用RadioButtons,则RadioButtons应该是RadioGroup的直接子代。

  2. 我检查了我的第一个条件,它是正确的,但是仍然选中了所有单选按钮,并且当给出id并运行项目时,它会自动解决我的问题。

答案 6 :(得分:0)

我注意到,如果不将id设置为单选按钮,则无法进行单选。

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

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

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