android 实现两组互斥的RadioGroup--手记

今天碰到RadioGroup多行多列问题,网上大多都是重写RadioGroup来实现,现在来提供另一种思路。

通过监听radiobutton的选中事件,调用RadioGroup.check( int id )方法 ,状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作


布局:

<RadioGroup
        android:id="@+id/report_radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioBtn_goods"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="货物问题" />

        <RadioButton
            android:id="@+id/radioBtn_weather"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="异常天气" />

        <RadioButton
            android:id="@+id/radioBtn_roads"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="交通管制" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/report_radioGroup2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioBtn_scheduling"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="临时调度" />

        <RadioButton
            android:id="@+id/radioBtn_car"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="车辆故障" />

        <RadioButton
            android:id="@+id/radioBtn_other"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="其他事件" />
    </RadioGroup>

绑定id

    @BindView(R.id.report_radioGroup1)
    RadioGroup report_radioGroup1;
    @BindView(R.id.report_radioGroup2)
    RadioGroup report_radioGroup2;

    @BindView(R.id.radioBtn_goods)
    RadioButton radioBtn_goods;
    @BindView(R.id.radioBtn_weather)
    RadioButton radioBtn_weather;
    @BindView(R.id.radioBtn_roads)
    RadioButton radioBtn_roads;
    @BindView(R.id.radioBtn_scheduling)
    RadioButton radioBtn_scheduling;
    @BindView(R.id.radioBtn_car)
    RadioButton radioBtn_car;
    @BindView(R.id.radioBtn_other)
    RadioButton radioBtn_other;

radiobutton 设置监听

@OnCheckedChanged({R.id.radioBtn_goods,R.id.radioBtn_weather,R.id.radioBtn_roads,
                        R.id.radioBtn_scheduling,R.id.radioBtn_car,R.id.radioBtn_other})
    public void viewOnCheck(CompoundButton buttonView, boolean isChecked){
        switch (buttonView.getId()){
            case R.id.radioBtn_goods:
                if (isChecked){
                    report_radioGroup2.check(-1);
                    ToastUtils.show(radioBtn_goods.getText().toString(),0);
                }
                break;
            case R.id.radioBtn_weather:
                if (isChecked){
                    report_radioGroup2.check(-1);
                    ToastUtils.show(radioBtn_weather.getText().toString(),0);
                }
                break;
            case R.id.radioBtn_roads:
                if (isChecked){
                    report_radioGroup2.check(-1);
                    ToastUtils.show(radioBtn_roads.getText().toString(),0);
                }
                break;
            case R.id.radioBtn_scheduling:
                if (isChecked){
                    report_radioGroup1.check(-1);
                    ToastUtils.show(radioBtn_scheduling.getText().toString(),0);
                }
                break;
            case R.id.radioBtn_car:
                if (isChecked){
                    report_radioGroup1.check(-1);
                    ToastUtils.show(radioBtn_car.getText().toString(),0);
                }
                break;
            case R.id.radioBtn_other:
                if (isChecked){
                    report_radioGroup1.check(-1);
                    ToastUtils.show(radioBtn_other.getText().toString(),0);
                }
                break;
        }
    }



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值