RadioGroup自定义位置

如图:实现RadioGroup中的RadioButton换行实现单选。

通过继承RelativeLayout实现自定义RadioGroup,实现RadioButton的任意布局。

public class RelativeRadioGroup extends RelativeLayout implements CompoundButton.OnCheckedChangeListener {
    private int checkId = -1;
    private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
    public RelativeRadioGroup(Context context) {
        super(context);
    }
    public RelativeRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public RelativeRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //添加监听
        for (int i = 0; i < getChildCount(); i++) {
            View v = getChildAt(i);
            if (v instanceof RadioButton && !(v instanceof CompoundButton.OnCheckedChangeListener)) {
                ((RadioButton) v).setOnCheckedChangeListener(this);
            }
        }
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setCheck(buttonView.getId(), isChecked);
    }
    public void check(@IdRes int checkId) {
        if (checkId == -1 || this.checkId == checkId) {
            return;
        }
        setCheck(checkId, true);
    }
    public void clearCheck() {
        setCheck(-1, false);
    }
    public int getCheckedRadioButtonId() {
        return this.checkId;
    }
    /**
     * 设置选中状态
     */
    private void setCheck(@IdRes int checkId, boolean isChecked) {
        if (checkId != -1 && this.checkId == checkId) {
            return;
        }
        if (checkId != -1) {
            CompoundButton view = (CompoundButton) findViewById(checkId);
            //未选中的RadioButton被选中
            if (checkId != this.checkId && isChecked) {
                this.checkId = checkId;
                if (mChildOnCheckedChangeListener != null) {
                    mChildOnCheckedChangeListener.onCheckedChanged(view, true);
                }
                //某个RadioButton被选中,将其他的改为未选中
                for (int i = 0; i < getChildCount(); i++) {
                    View v = getChildAt(i);
                    if (v instanceof RadioButton && v.getId() != checkId) {
                        ((RadioButton) v).setChecked(false);
                    } else if (v instanceof RadioButton && v.getId() == checkId) {
                        ((RadioButton) v).setChecked(true);
                    }
                }
            }
            //被选中的RadioButton被取消选中
            if (checkId == this.checkId && !isChecked) {
                this.checkId = checkId;
                if (mChildOnCheckedChangeListener != null) {
                    mChildOnCheckedChangeListener.onCheckedChanged(view, false);
                }
            }
        } else {
            //清空所有选择
            if (this.checkId != -1) {
                this.checkId = -1;
                CompoundButton view = (CompoundButton) findViewById(this.checkId);
                //将选中的置为未选中
                if (view instanceof RadioButton) {
                    view.setChecked(false);
                }
            }
        }
    }
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener) {
        this.mChildOnCheckedChangeListener = mChildOnCheckedChangeListener;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值