android 自定义组件:购买数量,带减少增加按钮

对于大多数的商城APP项目,都会有商品数量的增加,减少按钮,对此记录下封装:
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher {

    private static final String TAG = "AmountView";
    private int position=-1;

    private int amount = 0; //购买数量
    private int goods_storage = 100; //商品库存
    private EditText etAmount;
    private Button btnDecrease;
    private Button btnIncrease;

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

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //组件布局
        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        etAmount = (EditText) findViewById(R.id.etAmount);
        btnDecrease = (Button) findViewById(R.id.btnDecrease);
        etAmount.setVisibility(GONE);
        btnDecrease.setVisibility(GONE);
        btnIncrease = (Button) findViewById(R.id.btnIncrease);
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);
        etAmount.setFocusable(false);
    }


    /**
     * 位置
     * @param position
     */
    public void setPosition(int position) {
        this.position = position;
    }

    /**
     * 设置库存方法
     * @param goods_storage
     */
    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
    }

    /**
     * 获取数量
     * @return
     */
    public int getAmount() {
        return amount;
    }

    /**
     * 设置数量
     * @param amount
     */
    public void setAmount(int amount) {
        this.amount = amount;
        etAmount.setText(this.amount + "");
        if (this.amount>=0){
            etAmount.setVisibility(VISIBLE);
            btnDecrease.setVisibility(VISIBLE);
        }
    }

    /**
     * 增加,减少事件监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btnDecrease) {
            if (amount > 0) {
                amount--;
                etAmount.setText(amount + "");
                if (amount==0){
                    amount=1;

//                    etAmount.setVisibility(GONE);
//                    btnDecrease.setVisibility(GONE);
                }
            }
        } else if (i == R.id.btnIncrease) {
            if (amount < goods_storage) {
                amount++;
                etAmount.setText(amount + "");
                if (amount>=0){
                    etAmount.setVisibility(VISIBLE);
                    btnDecrease.setVisibility(VISIBLE);
                }
            }
        }

        etAmount.clearFocus();

        if (mListener != null) {
            mListener.onAmountChange(this, amount,position);
        }
    }

    /**
     * 数量变化监听
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty())
            return;
        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }

    }

    /**
     * 自定义接口 监听数量变化,
     */
    private OnAmountChangeListener mListener;

    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount,int position);
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }
}


组件布局:(备注:增加减少的按钮的背景图,可自行设置,这里就不写了)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:showDividers="middle"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnDecrease"
        android:layout_width="20dp"
        android:layout_height="21dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@mipmap/minus"/>

    <EditText
        android:id="@+id/etAmount"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="9dp"
        android:layout_marginLeft="9dp"
        android:background="@null"
        android:inputType="number"
        android:gravity="center"
        android:textSize="15sp"
        android:textColor="@color/color_red_del"
        android:text="0"/>

    <Button
        android:id="@+id/btnIncrease"
        android:layout_width="20dp"
        android:layout_height="21dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@mipmap/add"/>
</LinearLayout>

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值