Android布局中有两个RadioGroup的RadioButton选中问题

我的布局中有两个RadioGroup,需要只选中一个,只需在选中后调用另一个RadioGroup的check(0)方法即可


        case R.id.radio_320:
			SystemSet.putVideoData(activity, "Video", 7);
			radioGroup4.check(0);
			break;
		case R.id.radio_480:
			SystemSet.putVideoData(activity, "Video", 4);
			radioGroup4.check(0);
			break;
		case R.id.radio_720:
			SystemSet.putVideoData(activity, "Video", 5);
			radioGroup3.check(0);
			break;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要实现不在同一个 RadioGroup两个 RadioButton 只能选中其中一个的话,可以通过以下两种方式来实现: 1. 使用 RadioGroup 和 LinearLayout 组合实现: 在布局文件中,将两个 RadioButton 放在同一个 LinearLayout 中,然后再将 LinearLayout 放在 RadioGroup 中,这样就可以实现两个 RadioButton 只能选中其中一个的效果。具体代码如下: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" /> </LinearLayout> </RadioGroup> ``` 2. 使用自定义属性实现: 在布局文件中,给两个 RadioButton 设置同一个自定义属性,然后在代码中监听两个 RadioButton 的点击事件,当一个 RadioButton选中时,将另一个 RadioButton选中状态设置为未选中。具体代码如下: 在 res/values/attrs.xml 文件中定义自定义属性: ```xml <declare-styleable name="CustomRadioButton"> <attr name="exclusive" format="boolean" /> </declare-styleable> ``` 在布局文件中,给两个 RadioButton 设置相同的自定义属性: ```xml <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" app:exclusive="true" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" app:exclusive="true" /> ``` 在代码中,监听两个 RadioButton 的点击事件,并在事件中将另一个 RadioButton选中状态设置为未选中: ```java RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); radioButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton2.setChecked(false); } }); radioButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton1.setChecked(false); } }); ``` 以上是两种实现不在同一个 RadioGroup两个 RadioButton 只能选中其中一个的方法,您可以根据自己的需求选择其中一种。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值