android RadioGroup设置某一个被选中

见码滚

1                 mPriorityRadioGroup.clearCheck();
2                 mStatusRadioGroup.clearCheck();
3                 RadioButton r1 = (RadioButton)findViewById(R.id.statusNotDone);
4                 r1.setChecked(true);
5                 RadioButton r2 = (RadioButton)findViewById(R.id.medPriority);
6                 r2.setChecked(true);

 

转载于:https://www.cnblogs.com/hitnoah/p/4673189.html

如果要实现不在同一个 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、付费专栏及课程。

余额充值