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

F_X:

我已经在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

詹基·加迪亚(Janki Gadhiya):

由于RadioGroup中的TableRow,它无法正常工作。由于单选按钮之间没有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>

希望这可以帮助。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何确保一次仅运行一个Bash脚本实例?

是否可以使一个方法仅执行一次?

如何在Achor上一次仅显示一个AR对象?

如何确保一个按钮仅被单击一次“ n”次?Android Studio

一次仅允许一个异步操作

primeng-一次仅选择一个复选框项

jQuery如何仅一次声明一个选择器函数?

如何选择数组的多个html元素并将它们全部一次设置为一个函数?

无法使用角形材料2一次仅选择一个单选按钮

CoreUI一次仅一个下拉列表

如何使CAEmitterLayer一次仅生成一个单元格迅速4

转换一个div仅一次

如何更新一个表以设置从另一个表中选择的位置?

如何确保只能从4个不同的radioButton组中选择一个radioButton R闪亮

一次仅下拉一个菜单

如何一次仅检查一个单选按钮(其他按钮禁用)

应用发明者仅从列表中选择一个随机项一次

我们可以允许用户一次仅在SSRS中选择一个参数吗?

一次仅一个线程C#

一次仅选择一个区域

函数一次仅返回一个输出

tkinter中仅允许选择一个RadioButton

OpenMP一次仅执行一个线程

如何在两个模式中选择“第一次出现”,包括“第一次”?

如何在仅使用一个单元格的一个视图上设置与在 objc 中选择段按钮不同的 UITableView 单元格宽度

姜戈选择。如何将选项设置为仅选择一次

使用 Corona SDK 在循环中一次仅选择一个矩形

如何仅在反应中选择一次选项?

如何从android的RecylcerView中的radiogroup中选择一个单选按钮?