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

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

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

本文实例讲述了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,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,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 before,int count)    {    }  }}

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

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

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

总结

以上是内存溢出为你收集整理的Android实现仿淘宝购物车增加和减少商品数量功能demo示例全部内容,希望文章能够帮你解决Android实现仿淘宝购物车增加和减少商品数量功能demo示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存