您的位置:首页 > 移动开发 > Android开发

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

2018-03-20 18:24 543 查看
对于大多数的商城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>

效果图:

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