android 中关于多行RadioGroup只选择一个RadioButton的解决方法

今天博主在项目中遇到了一个问题,有多行数据用RadioGroup来显示,但是,只能选中这个多行RadioGroup中的一个radiobutton,刚开始我想的是用一个radiogroup做最外层,然后里面用LinearLayout来包裹这些radiobutton,以便于进行orientation属性的设置,但是,这样设置完成后,却不能实现博主的功能,无奈,只得自定义。代码如下:public cl...
摘要由CSDN通过智能技术生成

今天博主在项目中遇到了一个问题,有多行数据用RadioGroup来显示,但是,只能选中这个多行RadioGroup中的一个radiobutton,刚开始我想的是用一个radiogroup做最外层,然后里面用LinearLayout来包裹这些radiobutton,以便于进行orientation属性的设置,但是,这样设置完成后,却不能实现博主的功能,无奈,只得自定义。代码如下:

public class RadioGroupUtils extends RadioGroup {
    private OnCheckedChangeListener onCheckedChangeListener;

    public RadioGroupUtils(Context context) {
        super(context);
    }

    public RadioGroupUtils(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        onCheckedChangeListener = listener;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void addView(final View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof ViewGroup) {
            int childCount = ((ViewGroup) child).getChildCount();
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果要实现不在同一个 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 只能选一个的方法,您可以根据自己的需求选择一种。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值