自定义购物车加减view

public class NumberAddSubView extends LinearLayout implements View.OnClickListener {
    private Button btn_sub;
    private Button btn_add;
    private TextView tv_num;
    private Context mContext;

    /**
     * 设置默认值
     */
    private int value = 1;
    private int minValue = 1;
    private int maxValue = 5;

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

    public NumberAddSubView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    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_sub.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) {
//            Toast.makeText(mContext,"加",Toast.LENGTH_SHORT).show();
            addNum();
            if (onButtonClickListenter != null) {
                onButtonClickListenter.onButtonAddClick(v, value);
            }
        }
    }

    /**
     * 减少数据
     */
    private void subNum() {
        if (value > minValue) {
            value = value - 1;
            tv_num.setText(value + "");
        }
    }

    /**
     * 添加数据
     */
    private void addNum() {
        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);
    }

    private OnButtonClickListenter onButtonClickListenter;

    public void setOnButtonClickListenter(OnButtonClickListenter onButtonClickListenter) {
        this.onButtonClickListenter = onButtonClickListenter;
    }
}
XML---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="25dp"
    android:background="@drawable/selector_number_add_sub"
    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>
自定义属性:
<?xml version="1.0" encoding="utf-8"?>
<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>
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:color="#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>
selector_number_add_sub.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="#dddddd" />
    <solid android:color="#FFFFFF" />
</shape>
代码:
number_add_sub_view.setOnButtonClickListenter(new NumberAddSubView.OnButtonClickListenter() {
    @Override
    public void onButtonAddClick(View view, int value) {
        //数量+触发
    }

    @Override
    public void onButtonSubClick(View view, int value) {
        //数量-触发
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值