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

本文实例讲述了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程序设计有所帮助。

学习会使你永远立于不败之地。

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

相关文章:

你感兴趣的文章:

标签云: