手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android实现仿淘宝购物车增加和减少商品数量功能demo示例
Android实现仿淘宝购物车增加和减少商品数量功能demo示例
摘要:本文实例讲述了Android实现仿淘宝购物车增加和减少商品数量功能。分享给大家供大家参考,具体如下:在前面一篇《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,如下图所示:

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

接着,我们来编写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实现仿淘宝购物车增加和减少商品数量功能demo示例2

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

【Android实现仿淘宝购物车增加和减少商品数量功能demo示例】相关文章:

Android实现发短信/彩信功能

Android手势识别简单封装类

Android 倒影算法的实现代码

Android实现隐藏状态栏和标题栏

Android开发简单的乘法计算器,并添加退出菜单

Android中实现水平滑动(横向滑动)ListView示例

Android 使用Gallery实现3D相册(附效果图+Demo源码)

android开发基础教程—文件存储功能实现

android 照相功能的简单实例

Android 异步加载图片分析总结

精品推荐
分类导航