当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现仿淘宝购物车增加和减少商品数量功能demo示例

Android实现仿淘宝购物车增加和减少商品数量功能demo示例

2019年07月24日  | 移动技术网IT编程  | 我要评论

男子在森林散步突遇神秘生物,贷款利息计算器,修真的巨龙txt

本文实例讲述了android实现仿淘宝购物车增加和减少商品数量功能。分享给大家供大家参考,具体如下:

在前面一篇《android实现的仿淘宝购物车demo示例》中,小编简单的介绍了如何使用listview来实现购物车,但是仅仅是简单的实现了列表的功能,随之而来一个新的问题,买商品的时候,我们可能不止想买一件商品,想买多个,或许有因为某种原因点错了,本来想买一件来着,小手不小心抖了一下,把数量错点成了三个,这个时候就涉及到一个新的功能,那就是增加和减少商品的数量,今天这篇博文,小编就来和小伙伴们分享一下,如何实现淘宝购物车中增加和减少商品数量的demo。

首先,我们来布局xml文件,具体代码如下所示:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".mainactivity" >
  <!-- 整体布局,包括增加和减少商品数量的符号以及中间的商品数量 -->
  <linearlayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!-- 减少商品数量的布局 -->
    <button
      android:id="@+id/addbt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textcolor="#0157d3"
      android:text="-">
    </button>
    <!-- 商品数量的布局 -->
    <edittext
      android:id="@+id/edt"
      android:text="0"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </edittext>
    <!-- 增加商品数量的布局 -->
    <button
      android:id="@+id/subbt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textcolor="#0157d3"
      android:text="+">
    </button>
    <!-- 显示商品数量的布局 -->
    <textview
      android:id="@+id/ttt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </textview>
  </linearlayout>
</relativelayout>

我们来看一下xml布局的页面会是什么样子的nie,如下图所示:

接着,我们来编写java类里面的代码,具体代码如下所示:

package jczb.shoping.ui;
import android.r.string;
import android.app.activity;
import android.os.bundle;
import android.text.editable;
import android.text.textwatcher;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;
public class shoppingcartitemactivity extends activity {
  private button btadd, btreduce;
  private edittext edtnumber;
  int num=0; //数量
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_shoppingcart_item);
    btadd=(button)findviewbyid(r.id.cart_pro_reduce);
    btreduce=(button) findviewbyid(r.id.cart_pro_add);
    edtnumber=(edittext) findviewbyid(r.id.cart_pro_count);
    btadd.settag("+");
    btreduce.settag("-");
    //设置输入类型为数字
    edtnumber.setinputtype(android.text.inputtype.type_class_number);
    edtnumber.settext(string.valueof(num));
    setviewlistener();
  }
  /**
   * 设置文本变化相关监听事件
   */
  private void setviewlistener()
  {
    btadd.setonclicklistener(new onbuttonclicklistener());
    btreduce.setonclicklistener(new onbuttonclicklistener());
    edtnumber.addtextchangedlistener(new ontextchangelistener());
  }
  /**
   * 加减按钮事件监听器
   *
   *
   */
  class onbuttonclicklistener implements onclicklistener
  {
    @override
    public void onclick(view v)
    {
      string numstring = edtnumber.gettext().tostring();
      if (numstring == null || numstring.equals(""))
      {
        num = 0;
        edtnumber.settext("0");
      } else
      {
        if (v.gettag().equals("-"))
        {
          if (++num < 0) //先加,再判断
          {
            num--;
            toast.maketext(shoppingcartitemactivity.this, "请输入一个大于0的数字",
                toast.length_short).show();
          } else
          {
            edtnumber.settext(string.valueof(num));
          }
        } else if (v.gettag().equals("+"))
        {
          if (--num < 0) //先减,再判断
          {
            num++;
            toast.maketext(shoppingcartitemactivity.this, "请输入一个大于0的数字",
                toast.length_short).show();
          } else
          {
            edtnumber.settext(string.valueof(num));
          }
        }
      }
    }
  }
  /**
   * edittext输入变化事件监听器
   */
  class ontextchangelistener implements textwatcher
  {
    @override
    public void aftertextchanged(editable s)
    {
      string numstring = s.tostring();
      if(numstring == null || numstring.equals(""))
      {
        num = 0;
      }
      else {
        int numint = integer.parseint(numstring);
        if (numint < 0)
        {
          toast.maketext(shoppingcartitemactivity.this, "请输入一个大于0的数字",
              toast.length_short).show();
        } else
        {
          //设置edittext光标位置 为文本末端
          edtnumber.setselection(edtnumber.gettext().tostring().length());
          num = numint;
        }
      }
    }
    @override
    public void beforetextchanged(charsequence s, int start, int count,
        int after)
    {
    }
    @override
    public void ontextchanged(charsequence s, int start, int before,
        int count)
    {
    }
  }
}

最后,我们来看一下运行效果,如下图所示:

更多关于android相关内容感兴趣的读者可查看本站专题:《android布局layout技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网