200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 仿淘宝购物车demo 增加和减少商品数量

仿淘宝购物车demo 增加和减少商品数量

时间:2024-01-18 06:42:24

相关推荐

仿淘宝购物车demo   增加和减少商品数量

分享一下我老师大神的人工智能教程!零基础,通俗易懂!/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

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

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

<RelativeLayout xmlns:android="/apk/res/android" xmlns:tools="/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{@Overridepublic 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{@Overridepublic 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;}}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after){}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count){}}}

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

小编寄语:该博文小编主要简单的介绍了淘宝中如何实现增加和减少商品数量的小例子,希望可以帮助到有需要的小伙伴们,还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段实习的日子里,安卓带给小编的种种的惊喜。

给我老师的人工智能教程打call!/jiangjunshow

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。