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

F_X*_*F_X 15 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();
                }
            }

        });
Run Code Online (Sandbox Code Playgroud)

这里是我的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>
Run Code Online (Sandbox Code Playgroud)

编辑1:答案:如果你不想在一次选择单选按钮,所以不要使用Tablerow

Jan*_*iya 20

由于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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.:)

  • 顺便说一句,我使用 TableRow 在 2 行中创建一个单选按钮,那么如何在没有 tablerow 的情况下使其成为 2rwos? (2认同)

Ras*_*sel 6

我注意到如果不设置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>
Run Code Online (Sandbox Code Playgroud)


Vis*_*iya 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>
Run Code Online (Sandbox Code Playgroud)

并在java文件中

 RadioGroup radioGroup;



radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
Run Code Online (Sandbox Code Playgroud)

什么时候做某事

 if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)


Viv*_*mar 5

我遇到了同样的问题,发现我犯了错误,我在下面描述了它:

  1. 如果您在 RadioGroup 中使用 RadioButtons,那么RadioButtons should be direct child of RadioGroup.

  2. 检查你的第一个条件,如果它是正确的然后give id to RatioButton运行项目,它会自动解决你的问题。