Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 仿淘寶購物車:商品數量增減功能實作

編輯:關於 Android 程式設計

本文延續前一篇《Android 實現的仿淘寶購物車 demo》,示範如何為購物車加入商品數量的增加與減少功能。

先前那篇只用 listview 完成了列表的基本呈現,但實際購物時,使用者往往不只買一件;也可能手滑把數量點錯,這時候就需要「增減數量」的功能。本篇就來實作這個部分。

一、版面配置

先來看 XML 布局,用「減號—數量—加號」的水平排列組成數量控制區:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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> 

布局畫面如下:

二、Java 邏輯

接著撰寫 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) { } } } 

程式透過 tag 區分加號與減號按鈕,點擊時先增減再判斷是否為負數,避免數量降到不合理的範圍;同時監聽 EditText 的文字變化,讓使用者也能直接輸入數量。

運行效果如下:

以上就是商品數量增減功能的完整實作,把它套進購物車的 item 布局裡,就能獲得與淘寶相近的操作體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved