如何仅设置一个RadioButton一次可以在RadioGroup中选择 [英] How to set only one RadioButton Can be selected at the time in RadioGroup

查看:89
本文介绍了如何仅设置一个RadioButton一次可以在RadioGroup中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?

我正在使用片段

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代码

here my xml code for RG and 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

推荐答案

由于TableRow而无法正常工作在RadioGroup内部。因为它们之间的TableRow,所以未将所有RadioButton分组在一起。

It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.

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

只需像这样更改代码即可使用:

Just change your code like this it will work :

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

希望这会有所帮助。 :)

Hope this helps. :)

这篇关于如何仅设置一个RadioButton一次可以在RadioGroup中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆