android单选按钮实现只能选一个,android – 如何只设置一个RadioButton可以在RadioGroup中选择...

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@ id/RGroup">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Abdominal"

android:id="@ id/Abdominal"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Arm"

android:id="@ id/Arm"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Back"

android:id="@ id/Back" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Chest"

android:id="@ id/Chest"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Leg"

android:id="@ id/Leg"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Shoulder"

android:id="@ id/Shoulder"/>

编辑1:答案:

如果你不想在一次选择单选按钮,所以不要使用Tablerow

解决方法:

由于RadioGroup中的TableRow,它无法正常工作.由于它们之间的TableRow,所有RadioButton都没有组合在一起.

RadioButton应该是RadioGroup的直接子节点,否则分组不起作用.

只需更改您的代码就可以了:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@ id/RGroup">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Abdominal"

android:id="@ id/Abdominal"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Arm"

android:id="@ id/Arm"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Back"

android:id="@ id/Back" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Chest"

android:id="@ id/Chest"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Leg"

android:id="@ id/Leg"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Shoulder"

android:id="@ id/Shoulder"/>

希望这可以帮助. 🙂来源:https://www.icode9.com/content-4-487151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值