RadioGroup的简单封装

本文介绍了一个简单的自定义单行多列选择器类的实现方式,包括初始化视图、设置数据源、单选操作等功能。适用于需要在布局中实现多选项选择场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadioGroup的简单封装 如代码:

/**
 * @outher 孙磊磊
 * create at 2015/12/29 10:44
 * description 单行多列选择器
 */
public class SelectRadioGroup extends RadioGroup implements CompoundButton.OnCheckedChangeListener {
    private ChecdChangeListener mListener;
    private int mSelectPosition;
    private int mItenWidth;

    public SelectRadioGroup(Context context) {
        this(context, null);
    }

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

    private void initView() {
        WindowManager wm = (WindowManager) getContext()

                .getSystemService(Context.WINDOW_SERVICE);

        mItenWidth = (wm.getDefaultDisplay().getWidth() - 2 * DensityUtils.dp2px(getContext(), 5)) / 3;
        setGravity(Gravity.CENTER_VERTICAL);
        setOrientation(LinearLayout.HORIZONTAL);
        setPadding(getPx(5), getPx(5), getPx(5), getPx(5));
        setItem("item1", "item2", "item3");
    }

    /**
     * @outher 孙磊磊
     * create at 2015/12/29 14:49
     * description 提供数据源,设置item
     */
    public void setItem(String... strings) {
        removeAllViews();
        for (int i = 0; i < strings.length; i++) {
            addView(getItem(strings[i]));
        }
        setSelectItem(mSelectPosition);
    }

    /**
     * @outher 孙磊磊
     * create at 2015/12/29 14:48
     * description 初始化item,设置每个item宽度为屏幕三分之一左右
     */
    private RadioButton getItem(String str) {
        RadioButton radioButton = new RadioButton(getContext());
        LayoutParams lp = new RadioGroup.LayoutParams(mItenWidth, LayoutParams.MATCH_PARENT);
        radioButton.setLayoutParams(lp);
        radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
        radioButton.setGravity(Gravity.CENTER);
        radioButton.setTextSize(16);
        radioButton.setPadding(getPx(5), getPx(5), getPx(5), getPx(5));
        radioButton.setTextColor(getContext().getResources().getColor(R.color.white));
        radioButton.setOnCheckedChangeListener(this);
        radioButton.setText(str);
        return radioButton;
    }

    private int getPx(int dp) {
        return DensityUtils.dp2px(getContext(), dp);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            for (int i = 0; i < getChildCount(); i++) {
                RadioButton rb = (RadioButton) getChildAt(i);
                if (buttonView == rb) {
                    mSelectPosition = i;
                    if (mListener != null) {
                        mListener.seletItem(i);
                    }
                    rb.setTextColor(getContext().getResources().getColor(R.color.white));
                    rb.setBackgroundResource(R.drawable.round_orange);
                } else {
                    rb.setTextColor(getContext().getResources().getColor(R.color.grey_color));
                    rb.setBackgroundColor(getResources().getColor(R.color.bg_def));
                }
            }
        }
    }

    public interface ChecdChangeListener {
        public void seletItem(int position);
    }

    public void setListener(ChecdChangeListener listener) {
        this.mListener = listener;
    }

    /**
     * @outher 孙磊磊
     * create at 2015/12/29 14:47
     * description 设置指定的item
     */
    public void setSelectItem(int position) {
        if (position >= 0 && position < getChildCount()) {
            RadioButton rb = (RadioButton) getChildAt(position);
//            rb.setSelected(true);
            rb.performClick();
        }
    }

    /**
     * @outher 孙磊磊
     * create at 2015/12/29 14:47
     * description 返回当前选定的item下标
     */
    public int getSelectIndex() {
        return mSelectPosition;
    }
}

使用时直接调用方法 
public void setItem(String... strings);
不限定item个数,不指定时默认为三个,可嵌套在HorizontalScrollView内部,用于多选项滑动
效果图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值