欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 购物车加减功能的实现代码

程序员文章站 2022-06-09 08:28:43
android 实现购物车加减功能,效果图如下所示:public class adderview extends linearlayout implements view.onclicklistene...

android 实现购物车加减功能,效果图如下所示:

Android 购物车加减功能的实现代码

public class adderview extends linearlayout implements view.onclicklistener, textwatcher {
 private int amount = 0; //购买数量
 private int goods_storage = integer.max_value; //商品库存
 private onamountchangelistener mlistener;
 private edittext etamount;
 private button btndecrease;
 private button btnincrease;
 public adderview(context context) {
  this(context, null);
 }
 public adderview(context context, attributeset attrs) {
  super(context, attrs);
  layoutinflater.from(context).inflate(r.layout.number_adder, this);
  etamount = (edittext) findviewbyid(r.id.etamount);
  btndecrease = (button) findviewbyid(r.id.btndecrease);
  btnincrease = (button) findviewbyid(r.id.btnincrease);
  btndecrease.setonclicklistener(this);
  btnincrease.setonclicklistener(this);
  etamount.addtextchangedlistener(this);
  typedarray obtainstyledattributes = getcontext().obtainstyledattributes(attrs, r.styleable.amountview);
  int btnwidth = obtainstyledattributes.getdimensionpixelsize(r.styleable.amountview_btnwidth, 100);
  int tvwidth = obtainstyledattributes.getdimensionpixelsize(r.styleable.amountview_tvwidth, 200);
  int tvtextsize = obtainstyledattributes.getdimensionpixelsize(r.styleable.amountview_tvtextsize, 0);
  int btntextsize = obtainstyledattributes.getdimensionpixelsize(r.styleable.amountview_btntextsize, 0);
  obtainstyledattributes.recycle();
  layoutparams btnparams = new layoutparams(btnwidth, layoutparams.match_parent);
  btndecrease.setlayoutparams(btnparams);
  btnincrease.setlayoutparams(btnparams);
  if (btntextsize != 0) {
   btndecrease.settextsize(typedvalue.complex_unit_px, btntextsize);
   btnincrease.settextsize(typedvalue.complex_unit_px, btntextsize);
  }
  layoutparams textparams = new layoutparams(tvwidth, layoutparams.match_parent);
  etamount.setlayoutparams(textparams);
  if (tvtextsize != 0) {
   etamount.settextsize(tvtextsize);
  }
 }
 public void setonamountchangelistener(onamountchangelistener onamountchangelistener) {
  this.mlistener = onamountchangelistener;
 }
 public void setgoods_storage(int goods_storage) {
  this.goods_storage = goods_storage;
 }
 public void settextcount(int count){
  this.amount = count;
  this.etamount.settext(amount+"");
 }
 @override
 public void onclick(view v) {
  int i = v.getid();
  if (i == r.id.btndecrease) {
   if (amount > 0) {
    amount--;
    etamount.settext(amount + "");
   }
  } else if (i == r.id.btnincrease) {
   if (amount < goods_storage) {
    amount++;
    etamount.settext(amount + "");
   }
  }
  etamount.clearfocus();
  if (mlistener != null) {
   mlistener.onamountchange(this, amount);
  }
 }
 @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;
  }
  if (amount == 0){
//   btndecrease.setbackgroundresource(r.drawable.jian);
  }
  if (amount > 0){
//   btndecrease.setbackgroundresource(r.drawable.lvjian);
  }
  if (mlistener != null) {
   mlistener.onamountchange(this, amount);
  }
 }
 public interface onamountchangelistener {
  void onamountchange(view view, int amount);
 }
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.autolinearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="90px"
 android:focusable="true"
 android:divider="@drawable/divder"
 android:background="@drawable/bg_amout_layout"
 android:showdividers="middle"
 android:orientation="horizontal">
 <button
  android:id="@+id/btndecrease"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:gravity="center"
  android:background="@drawable/jian"/>
 <edittext
  android:id="@+id/etamount"
  android:layout_width="200px"
  android:layout_height="match_parent"
  android:minwidth="150px"
  android:layout_weight="2"
  android:background="@null"
  android:inputtype="number"
  android:textsize="13sp"
  android:text="0"
  android:gravity="center"/>
 <button
  android:id="@+id/btnincrease"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:gravity="center"
  android:background="@drawable/jia"/>
</com.zhy.autolayout.autolinearlayout>

到此这篇关于android 购物车加减功能的实现代码的文章就介绍到这了,更多相关android 购物车加减内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!