RadioGroup

简介

安卓自带RadioGroup方向水平。

多行

效果

代码

HorizontalVerticalRadioGroup

package widget.button;

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;

import util.LogUtils;

/**
 * Created on 2019/5/23.
 *
 * @author 郑少鹏
 * @desc 水平垂直RadioGroup
 */
public class HorizontalVerticalRadioGroup extends RadioGroup {
    private OnCheckedChangeListener mOnCheckedChangeListener;

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

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

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public void addView(final View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof LinearLayout) {
            int childCount = ((LinearLayout) child).getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = ((LinearLayout) child).getChildAt(i);
                if (view instanceof RadioButton) {
                    final RadioButton button = (RadioButton) view;
                    button.setOnTouchListener((v, event) -> {
                        button.setChecked(true);
                        checkRadioButton(button);
                        if (mOnCheckedChangeListener != null) {
                            mOnCheckedChangeListener.onCheckedChanged(HorizontalVerticalRadioGroup.this, button.getId());
                        }
                        return true;
                    });
                }
            }
        }
        super.addView(child, index, params);
    }

    private void checkRadioButton(RadioButton radioButton) {
        View child;
        int radioCount = getChildCount();
        for (int i = 0; i < radioCount; i++) {
            child = getChildAt(i);
            if (child instanceof RadioButton) {
                if (child == radioButton) {
                    // do nothing
                    LogUtils.e("child == radioButton");
                } else {
                    ((RadioButton) child).setChecked(false);
                }
            } else if (child instanceof LinearLayout) {
                int childCount = ((LinearLayout) child).getChildCount();
                for (int j = 0; j < childCount; j++) {
                    View view = ((LinearLayout) child).getChildAt(j);
                    if (view instanceof RadioButton) {
                        final RadioButton button = (RadioButton) view;
                        if (button == radioButton) {
                            // do nothing
                            LogUtils.e("child == radioButton");
                        } else {
                            button.setChecked(false);
                        }
                    }
                }
            }
        }
    }
}

布局

<widget.button.HorizontalVerticalRadioGroup
    android:id="@+id/rgTimeCoin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/d12"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbTimeCoin1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blue_rabutton_selector"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:paddingBottom="@dimen/d6"
            android:paddingLeft="@dimen/d12"
            android:paddingRight="@dimen/d12"
            android:paddingTop="@dimen/d6"
            android:text="100"
            android:textColor="@drawable/tv_blue_white_en_selector"
            android:textSize="@dimen/s15" />
        ...       
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/d12"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbTimeCoin1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blue_rabutton_selector"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:paddingBottom="@dimen/d6"
            android:paddingLeft="@dimen/d12"
            android:paddingRight="@dimen/d12"
            android:paddingTop="@dimen/d6"
            android:text="100"
            android:textColor="@drawable/tv_blue_white_en_selector"
            android:textSize="@dimen/s15" />
        ...       
    </LinearLayout>
</widget.button.HorizontalVerticalRadioGroup>

主代码

rgTimeCoin.setOnCheckedChangeListener((group, checkedId) -> {
    switch (checkedId) {
        case R.id.rbTimeCoin1:
            timeCoin = rbTimeCoin1.getText().toString();
            break;
        case R.id.rbTimeCoin2:
            timeCoin = rbTimeCoin2.getText().toString();
            break;
        case R.id.rbTimeCoin3:
            timeCoin = rbTimeCoin3.getText().toString();
            break;
        case R.id.rbTimeCoin4:
            timeCoin = rbTimeCoin4.getText().toString();
            break;
        case R.id.rbTimeCoin11:
            timeCoin = rbTimeCoin11.getText().toString();
            break;
        case R.id.rbTimeCoin12:
            timeCoin = rbTimeCoin12.getText().toString();
            break;
        case R.id.rbTimeCoin13:
            timeCoin = rbTimeCoin13.getText().toString();
            break;
        case R.id.rbTimeCoin14:
            timeCoin = rbTimeCoin14.getText().toString();
            break;
        default:
            break;
    }
});
不足

不可实现取选。

取选

单行
radioGroup.clearCheck();
多行
日志
StackOveflowError
分析

RadioGroup多行显即多RadioGroup切换,通RadioGroup之clearCheck()操作。clearCheck()前RadioGroup设监听则执行报StackOveflowError错。

解决
radioGroupOne.setOnCheckedChangeListener(new OneCheckedChangeListener());
radioGroupTwo.setOnCheckedChangeListener(new TwoCheckedChangeListener());\

/**
 * 一筛选
 */
class OneCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

    /**
     * <p>Called when the checked radio button has changed. When the
     * selection is cleared, checkedId is -1.</p>
     *
     * @param group     the group in which the checked radio button has changed
     * @param checkedId the unique identifier of the newly checked radio button
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        radioGroupTwo.setOnCheckedChangeListener(null);
        radioGroupTwo.clearCheck();
        radioGroupTwo.setOnCheckedChangeListener(new TwoCheckedChangeListener());
        switch (checkedId) {
            // 婴儿(0-1)
            case R.id.bottomSheetDialogMemberScreenMrbBaby:
                break;
            ....
            default:
                break;
        }
    }
}

/**
 * 二筛选
 */
class TwoCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

    /**
     * <p>Called when the checked radio button has changed. When the
     * selection is cleared, checkedId is -1.</p>
     *
     * @param group     the group in which the checked radio button has changed
     * @param checkedId the unique identifier of the newly checked radio button
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        radioGroupOne.setOnCheckedChangeListener(null);
        radioGroupOne.clearCheck();
        radioGroupOne.setOnCheckedChangeListener(new OneCheckedChangeListener());
        switch (checkedId) {
            // 少年(12-18)
            case R.id.bottomSheetDialogMemberScreenMrbEarlyYouth:
                break;
            ....        
            default:
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snpmyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值