Android實現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能demo示例
本文實例講述了Android實現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能。分享給大家供大家參考,具體如下:
在前面一篇《Android實現(xiàn)的仿淘寶購物車demo示例》中,小編簡單的介紹了如何使用listview來實現(xiàn)購物車,但是僅僅是簡單的實現(xiàn)了列表的功能,隨之而來一個新的問題,買商品的時候,我們可能不止想買一件商品,想買多個,或許有因為某種原因點錯了,本來想買一件來著,小手不小心抖了一下,把數(shù)量錯點成了三個,這個時候就涉及到一個新的功能,那就是增加和減少商品的數(shù)量,今天這篇博文,小編就來和小伙伴們分享一下,如何實現(xiàn)淘寶購物車中增加和減少商品數(shù)量的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" > <!-- 整體布局,包括增加和減少商品數(shù)量的符號以及中間的商品數(shù)量 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 減少商品數(shù)量的布局 --> <Button android:id="@+id/addbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0157D3" android:text="-"> </Button> <!-- 商品數(shù)量的布局 --> <EditText android:id="@+id/edt" android:text="0" android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText> <!-- 增加商品數(shù)量的布局 --> <Button android:id="@+id/subbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0157D3" android:text="+"> </Button> <!-- 顯示商品數(shù)量的布局 --> <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; //數(shù)量 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("-"); //設置輸入類型為數(shù)字 edtNumber.setInputType(android.text.InputType.TYPE_CLASS_NUMBER); edtNumber.setText(String.valueOf(num)); SetViewListener(); } /** * 設置文本變化相關監(jiān)聽事件 */ private void SetViewListener() { btAdd.setOnClickListener(new OnButtonClickListener()); btReduce.setOnClickListener(new OnButtonClickListener()); edtNumber.addTextChangedListener(new OnTextChangeListener()); } /** * 加減按鈕事件監(jiān)聽器 * * */ 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的數(shù)字", Toast.LENGTH_SHORT).show(); } else { edtNumber.setText(String.valueOf(num)); } } else if (v.getTag().equals("+")) { if (--num < 0) //先減,再判斷 { num++; Toast.makeText(ShoppingCartItemActivity.this, "請輸入一個大于0的數(shù)字", Toast.LENGTH_SHORT).show(); } else { edtNumber.setText(String.valueOf(num)); } } } } } /** * EditText輸入變化事件監(jiān)聽器 */ 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的數(shù)字", 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相關內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結》、《Android視圖View技巧總結》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android 自定義精美界面包含選項菜單 上下文菜單及監(jiān)聽詳解流程
這篇文章主要介紹了一個Android實例小項目,它包含了選項菜單、上下文菜單及其對應的監(jiān)聽事件,它很小,但這部分功能在Android開發(fā)中很常見,需要的朋友來看看吧2021-11-11Flutter?Widget開發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12Android應用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實例
這篇文章主要介紹了Android應用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實例,例子中包括動態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02Android自定義view實現(xiàn)左滑刪除的RecyclerView詳解
RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2022-11-11android-使用環(huán)信SDK開發(fā)即時通信功能(附源碼下載)
本篇文章主要介紹了android-使用環(huán)信SDK開發(fā)即時通信功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-12-12Android開發(fā)實現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細介紹了Android開發(fā)中實現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09