您的位置:首页 > 其它

购物车加减器--自定义view

2018-01-09 18:53 253 查看
NumberAddSubView

public class NumberAddSubView extends LinearLayout implements View.OnClickListener {

    private Button btn_sub;

    private Button btn_add;

    private TextView tv_num;

    private Context mContext;

    private OnButtonClickListenter onButtonClickListenter;

    public void setOnButtonClickListenter(OnButtonClickListenter onButtonClickListenter) {

        this.onButtonClickListenter = onButtonClickListenter;

    }

    /**

     * 设置默认值

     */

    private int value = 1;

    private int minValue = 1;

    //private int maxValue = 5;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

    public NumberAddSubView(Context context) {

        this(context, null);

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

    public NumberAddSubView(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

    public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        this.mContext = context;

        initView(context);

        //得到属性

        if (attrs != null) {

            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberAddSubView);

            //TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.NumberAddSubView, defStyleAttr, 0);

            int value = a.getInt(R.styleable.NumberAddSubView_value, 0);

            setValue(value);

            /*int maxValue = a.getInt(R.styleable.NumberAddSubView_maxValue, 0);

            setMaxValue(maxValue);*/

            /*int minValue = a.getInt(R.styleable.NumberAddSubView_minValue, 0);

            setMinValue(minValue);*/

            Drawable btnSubBackground = a.getDrawable(R.styleable.NumberAddSubView_btnSubBackground);

            if (btnSubBackground != null)

                btn_sub.setBackground(btnSubBackground);

            Drawable btnAddBackground = a.getDrawable(R.styleable.NumberAddSubView_btnAddBackground);

            if (btnAddBackground != null)

                btn_add.setBackground(btnAddBackground);

            Drawable textViewBackground = a.getDrawable(R.styleable.NumberAddSubView_textViewBackground);

            if (textViewBackground != null)

                tv_num.setBackground(textViewBackground);

            a.recycle();

        }

    }

    private void initView(Context context) {

        //第三个参数:把当前View加载到NumberAddSubView控件上

        View.inflate(context, R.layout.number_add_sub_view, this);

        btn_sub = (Button) findViewById(R.id.btn_sub);

        btn_add = (Button) findViewById(R.id.btn_add);

        tv_num = (TextView) findViewById(R.id.tv_num);

        btn_sub.setOnClickListener(this);

        btn_add.setOnClickListener(this);

    }

    public int getValue() {

        String val = tv_num.getText().toString();

        if (!TextUtils.isEmpty(val)) {

            value = Integer.parseInt(val);

        }

        return value;

    }

    public void setValue(int value) {

        this.value = value;

        tv_num.setText(value + "");

    }

    //设置最小值

    /*public int getMinValue() {

        return minValue;

    }

    public void setMinValue(int minValue) {

        this.minValue = minValue;

    }*/

    //设置最大值

    /*public int getMaxValue() {

        return maxValue;

    }

    public void setMaxValue(int maxValue) {

        this.maxValue = maxValue;

    }*/

    @Override

    public void onClick(View v) {

        if (v.getId() == R.id.btn_sub) {

            //Toast.makeText(mContext,"减",Toast.LENGTH_SHORT).show();

            subNum();

            if (onButtonClickListenter != null) {

                onButtonClickListenter.onButtonSubClick(v, value);

            }

        } else if (v.getId() == R.id.btn_add) {

            addNum();

            //Toast.makeText(mContext,"加",Toast.LENGTH_SHORT).show();

            if (onButtonClickListenter != null) {

                onButtonClickListenter.onButtonAddClick(v, value);

            }

        }

    }

    /**

     * 减少数据

     */

    private void subNum() {

        if (value > minValue) {

            value = value - 1;

            tv_num.setText(value + "");

        }else{

            Toast.makeText(mContext,"最少数量为1",Toast.LENGTH_SHORT).show();

        }

    }

    /**

     * 添加数据

     */

    private void addNum() {

        value = value + 1;

        tv_num.setText(value + "");

        //有最大值上限时需要

        /*if (value < maxValue) {

            value = value + 1;

            tv_num.setText(value + "");

        }*/

    }

    public interface OnButtonClickListenter {

        /**

         * 当增加按钮被点击的时候回调该方法

         *

         * @param view

         * @param value

         */

        public void onButtonAddClick(View view, int value);

        /**

         * 当减少按钮被点击的时候回调这个方法

         *

         * @param view

         * @param value

         */

        public void onButtonSubClick(View view, int value);

    }

}

atrrs.xml

<resources>

    <declare-styleable name="NumberAddSubView">

        <attr name="value" format="integer|reference"/>

        <attr name="minValue" format="integer|reference"/>

        <attr name="maxValue" format="integer|reference"/>

        <attr name="btnAddBackground" format="reference"/>

        <attr name="btnSubBackground" format="reference"/>

        <attr name="textViewBackground" format="reference"/>

    </declare-styleable>

</resources>

number_add_sub_view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="100dp"

    android:layout_height="30dp"

    android:orientation="horizontal">

    <Button

        android:id="@+id/btn_sub"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="match_parent"

        android:background="@drawable/bg_btn_style_white"

        android:text="一"

        android:textColor="#000000"

        android:textSize="14sp" />

    <TextView

        android:id="@+id/tv_num"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="match_parent"

        android:gravity="center"

        android:minWidth="100dp"

        android:text="1" />

    <Button

        android:id="@+id/btn_add"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="match_parent"

        android:background="@drawable/bg_btn_style_white"

        android:text="十"

        android:textColor="#000000"

        android:textSize="14sp" />

</LinearLayout>

bg_btn_style_white.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="false">

        <shape android:shape="rectangle">

            <corners android:radius="2.0dp" />

            <solid android:color="#7fd8d8d8" />

            <stroke android:width="1.0dp" android:color="#dddddd" />

        </shape>

    </item>

    <item android:state_pressed="true">

        <shape android:shape="rectangle">

            <corners android:radius="2.0dp" />

            <solid android:color="#ffd8d8d8" />

            <stroke android:width="1.0dp" android:col
a0d6
or="#ddd" />

        </shape>

    </item>

    <item>

        <shape android:shape="rectangle">

            <corners android:radius="2.0dp" />

            <solid android:color="#ffffff" />

            <stroke android:width="1.0dp" android:color="#dddddd" />

        </shape>

    </item>

</selector>

shape_number_add_sub.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="2dp" />

    <stroke

        android:width="1dp"

        android:color="#dddddd" />

    <solid android:color="#FFFFFF" />

</shape>

使用点击事件

ch.numberaddsubview.setOnButtonClickListenter(new NumberAddSubView.OnButtonClickListenter() {

            @Override

            public void onButtonAddClick(View view, int value) {

               

            }

            @Override

            public void onButtonSubClick(View view, int value) {

               

            }

        });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: