Android自定义RadioGroup

最近做项目时需要用到RadioGroup,发现Android原生的RadioGroup太丑了,所以自己写了一个,效果如下所示:Android自定义RadioGroup_第1张图片

其实就是由4个Button组成的LinearLayout,只是为了方便点击效果的切换所以封装了一下。代码如下:

package com.dy.erp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.dy.erp.R;
import com.dy.erp.util.DensityUtils;

/**
 * Created by dy on 2016/5/25 10:52.
 */
public class MyRadioButton extends LinearLayout {
    private Button[] btns;
    private int selectedPosition;
    private RadioBtnOnClick mRadioBtnOnClick;

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        //默认button数量为4,如果读者扩展的话,只需自定义attrs一个number,在xml里面填写属性
        //然后在代码中通过TypedArray获取number
        init(context, 4);
    }

    public MyRadioButton(Context context) {
        super(context);
        //默认button数量为4
        init(context, 4);

    }

    //自定义构造函数
    public MyRadioButton(Context context, int number) {
        super(context);
        init(context, number);
    }

    private void init(Context context, int number) {
        //默认排列方式为水平
        setOrientation(LinearLayout.HORIZONTAL);
//        LayoutInflater inflater = (LayoutInflater) context
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        inflater.inflate(R.layout.view_service, this);

        setViews(context, number);
    }

    private void setViews(Context context, int number) {
        btns = new Button[number];
        //新建number个Button
        for (int i = 0; i < number; i++) {
            final Button button = new Button(context);
            //如需修改button的text则可通过btns数组操作
            button.setText((i + 1) + "年");
            LinearLayout.LayoutParams params = new
                    LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
            //button的间隔,这里是默认水平排列方式,所以只写了left,right的margin
            //如果为竖直排列的话,则修改top,bottom
            params.setMargins(20, 0, 20, 0);
            params.weight = 1;
            //设置button的点击效果
            button.setBackgroundResource(R.drawable.selector_radio_btn);
            //button.setMinHeight((int) DensityUtils.px2dp(context, 20));

            button.setGravity(Gravity.CENTER);
            btns[i] = button;
            button.setTag(i);
            if (i == 0) {
                button.setSelected(true);
            }
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    //该button选中
                    view.setSelected(true);
                    //其他button则要修改为未选中
                    setSelectedFalse((int) view.getTag());
                    //当前选中的位置
                    selectedPosition = (int) view.getTag();
                    if (mRadioBtnOnClick != null) {
                        mRadioBtnOnClick.click((int) view.getTag());
                    }
                }
            });

            addView(button, params);
        }


    }
    //获取当前选中的位置
    public int getSelectedPosition() {
        return this.selectedPosition;
    }
    //设置当前选中的位置
    public void setSelectedPosition(int position) {
        btns[position].setSelected(true);
        setSelectedFalse(position);
        this.selectedPosition = position;
    }

    //修改未选中button的状态
    private void setSelectedFalse(int selectedPosition) {
        for (int i = 0; i < btns.length; i++) {
            if (i == selectedPosition)
                continue;
            if (btns[i] != null)
                btns[i].setSelected(false);
        }
    }

    //供外部设置button监听事件
    public void setRadioBtnOnClick(RadioBtnOnClick radioBtnOnClick) {
        this.mRadioBtnOnClick = radioBtnOnClick;
    }

    //button点击接口
    public interface RadioBtnOnClick {
        void click(int position);
    }

}

下面贴上button点击效果的xml文件,读者可随意修改,可控制button的形状,边框,颜色等等。

selector_radio_btn.xml




    
        
            

            

            
        
    
    
        
            

            

            
        
    


color.xml

<color name="deep_gray">#ff7F7F7Fcolor>

<color name="orange">#fffed000color>
<color name="white">#ffffffffcolor>


你可能感兴趣的:(android)