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>
效果图:
相关文章推荐
- 自定义组件:购买数量,带减少增加按钮
- 自定义组件:数量,带减少增加按钮
- 简单封装一个组件:购买数量,带减少增加按钮AmountView
- ECSHOP商品购买数量增加加减按钮插件(包含购物车商品数量增加减少,自动更新)
- Android自定义组件系列【14】——Android5.0按钮波纹效果实现
- android中ExpandableListView增加自定义组件
- vue2.0+stylus实现商品增加减少控制按钮组件
- Android 音量增加减少按钮事件
- Android中自定义ViewGroup使每行组件数量不确定,并拿到选中数据
- android - 自定义标题栏(在标题栏中增加按钮和文本居中)
- jquery按钮控制text文本框商品数量增加或减少
- 带有增加与减少按钮的数量选择控件QuantityView
- jquery按钮控制text文本框商品数量增加或减少
- 类似于购物车,点击按钮,购物数量的增加或者减少
- android - 自定义标题栏(在标题栏中增加按钮和文本居中)
- 带有增加与减少按钮的数量选择控件QuantityView
- android - 自定义标题栏(在标题栏中增加按钮和文本居中)
- Android自定义控件系列二:自定义开关按钮(一) 标签: Android自定义控件Java开关按钮自定义组件 2014-11-03 10:55 2735人阅读 评论(6) 收藏 举报 分类: J
- android - 自定义标题栏(在标题栏中增加按钮和文本居中)
- Android实现仿淘宝购物车增加和减少商品数量功能demo示例