当前位置: 首页 > 知识库问答 >
问题:

如何在RadioGroup中只选择一个RadioButton

弘靖琪
2023-03-14
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

共有1个答案

孙莫希
2023-03-14

它不起作用是因为放射组里的桌子。所有的RadioButtons不是分组在一起,因为它们之间有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>
 类似资料:
  • 我有一个RecycerView,其中每个项目都有3个radiobuttons分组在一个RadioGroup中。现在,用户只能在RecycerView中为每个项目选择一个radiobutton。但是我希望用户在整个RecycerView中只选择一个radiobutton。如何做到这一点? 这就是它目前的样子。 我希望在整个回收器视图中只检查1个radiobutton。如果选中了第一项中的第一个单选按

  • 我有一个RecycerView与RadioButton我只是想选择一个RadioButton不太多在同一时间和我的代码使它工作的很好,但当我重复选择从上到下的选择消失我如何才能修复它请谢谢。

  • 问题内容: 如何在Java中管理两个JRadioButton,以便一次只能选择其中一个?Java中是否有任何方法可以解决此问题,或者您需要构建自己的逻辑? 问题答案: 您必须将它们添加到ButtonGroup中 确保在使用新的构造函数创建按钮后适当添加此代码。

  • 问题内容: 我们选择带有Django 1.4测试和Selenium的元素,如下所示: (该类继承自)。 问题是有时有些元素没有文本,并且如果我们选择它,它就会失败(len为0)。如何选择没有文本的元素? 更新 :由于不起作用,我不得不断言两行以断言没有文本: 现在,我可以用一行声明相同的内容: 问题答案: 您可以使用XPath的 功能。

  • 在这里json 如何使用库boost获得更新的值4? 我知道怎么取这样一个“ts”值。但是我不明白如何取两个括号的值

  • 问题内容: 我只需要选择给定datetime变量的月份的第一天。 我知道使用这种代码非常容易: 但这不是很优雅,也可能不是很快。 有一个更好的方法吗?我正在使用SQL Server 2008。 问题答案: