RadioButton多行显示

日常使用单行的时候都是以列表的形式,如横向或者竖向,而一但需要多行显示的时候就不行了,如下面的效果图:
在这里插入图片描述

实现这样的效果图有以下几种思路:

  1. 使用RadioGroup竖向布局。中间嵌套LinearLayout来使用。但是这样RadioGroup会失去单选的功能,因为所有的RadioButton必须作为RadioGroup的子view才行。
  2. 分2行RadioGroup。使用RadioGroupsetOnCheckChangeListener来监听,当选中的radio是属于group1的时候,使用group2的clearCheck()方法清空group2的状态,反之亦然。但是由于使用clearCheck()方法时也会回调checkChange()方法,所以方法执行时无法判断是选中时回调的方法还是清空时回调的方法
  3. 重写RadioGroup。让其支持多行显示,但是这样太复杂了。
  4. 使用RadioButton数组。不使用RadioGroupsetOnCheckChange方法,使用RadioButtonsetOnCheckChange方法来手动管理radio的状态

我比较倾向第4种,以下是实现方式:

.xml文件

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rg_firstLine"
    android:layout_marginTop="@dimen/margin_xxlarge"
    android:orientation="horizontal">
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount0"
        android:checked="true"
        android:layout_marginStart="@dimen/margin_xxlarge"
        android:text="@string/label_no_discount" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount1"
        android:text="@string/label_discount1" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount2"
        android:text="@string/label_discount2" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount3"
        android:text="@string/label_discount3" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount4"
        android:text="@string/label_discount4" />
</RadioGroup>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rg_secondLine"
    android:layout_marginTop="@dimen/margin_large"
    android:orientation="horizontal">
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount5"
        android:layout_marginStart="@dimen/margin_xxlarge"
        android:text="@string/label_discount5" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount6"
        android:text="@string/label_discount6" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount7"
        android:text="@string/label_discount7" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount8"
        android:text="@string/label_discount8" />
    <RadioButton
        style="@style/discount"
        android:id="@+id/rb_discount9"
        android:text="@string/label_discount9" />
</RadioGroup>

.java文件

// 设置监听
for(RadioButton r : rb_firstLine){         // rb_firstLine为第一行的所有RadioButton
    r.setOnCheckedChangeListener(this);
}

for (RadioButton r : rb_secondLine){
    r.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {     // isChecked是必须判断的,不然会有几率出现点击了ui没选中
        uncheckRadio(buttonView.getId());
    }
}
// 选中某个radiobutton,反选其他所有radiobutton
private void uncheckRadio(int checkId) {
    for (RadioButton r : rb_firstLine) {
        if (r.getId() == checkId) {
            rg_secondLine.clearCheck();
            r.setChecked(true);
        }else {
            r.setChecked(false);
        }
    }
    for (RadioButton r : rb_secondLine) {
        if (r.getId() == checkId) {
            rg_firstLine.clearCheck();
            r.setChecked(true);

        }else {
            r.setChecked(false);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值