android studio 多行的RadioButton,多选一功能

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MultiRadioGroup extends LinearLayout {
    private RadioGroup.OnCheckedChangeListener mCheckChangeListener;
    private RadioGroup lastRadioGroup;
    private RadioGroup.OnCheckedChangeListener changeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton current = (RadioButton) group.findViewById(checkedId);
            if (current == null)
                return;
            if (lastRadioGroup != null && lastRadioGroup != group) {
                lastRadioGroup.clearCheck();
            }
            if (current.isChecked() && mCheckChangeListener != null)
                mCheckChangeListener.onCheckedChanged(group, checkedId);
            lastRadioGroup = group;
        }
    };

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (child instanceof RadioGroup) {
            ((RadioGroup) child).setOnCheckedChangeListener(changeListener);
        }
    }

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

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

    public MultiRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RadioGroup.OnCheckedChangeListener getCheckChangeListener() {
        return mCheckChangeListener;
    }

    public void setCheckChangeListener(RadioGroup.OnCheckedChangeListener mCheckChangeListener) {
        this.mCheckChangeListener = mCheckChangeListener;
    }
}

 

使用实例:

<?xml version="1.0" encoding="utf-8"?>
<com.example.mytestapp.MultiRadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/all_radioGroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <RadioButton
            android:id="@+id/fir"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="1"
            android:text="50元" />

        <RadioButton
            android:id="@+id/sec"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="2"
            android:text="100元" />

        <RadioButton
            android:id="@+id/thir"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="3"
            android:text="150元" />
    </RadioGroup>

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

        <RadioButton
            android:id="@+id/forth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="4"
            android:text="200元" />

        <RadioButton
            android:id="@+id/fifth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="5"
            android:text="250元" />

        <RadioButton
            android:id="@+id/sixth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="6"
            android:text="300元" />
    </RadioGroup>
</com.example.mytestapp.MultiRadioGroup>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值