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

android– 如何只设置一个RadioButton可以在RadioGroup中选择,第1张

概述我在radiogroup中创建了一个单选按钮,但是当我尝试运行应用程序时,可以一直选择所有单选按钮,并且一次只能选择如何设置一个单选按钮?我正在使用片段RadioGroupradioGroup=(RadioGroup)rootView.findViewById(R.id.RGroup);radioGroup.setOnCheckedChangeListener(n

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

解决方法:

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

希望这可以帮助. 总结

以上是内存溢出为你收集整理的android – 如何只设置一个RadioButton可以在RadioGroup中选择全部内容,希望文章能够帮你解决android – 如何只设置一个RadioButton可以在RadioGroup中选择所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存