Android實(shí)現(xiàn)仿淘寶購(gòu)物車(chē)增加和減少商品數(shù)量功能demo示例
本文實(shí)例講述了Android實(shí)現(xiàn)仿淘寶購(gòu)物車(chē)增加和減少商品數(shù)量功能。分享給大家供大家參考,具體如下:
在前面一篇《Android實(shí)現(xiàn)的仿淘寶購(gòu)物車(chē)demo示例》中,小編簡(jiǎn)單的介紹了如何使用listview來(lái)實(shí)現(xiàn)購(gòu)物車(chē),但是僅僅是簡(jiǎn)單的實(shí)現(xiàn)了列表的功能,隨之而來(lái)一個(gè)新的問(wèn)題,買(mǎi)商品的時(shí)候,我們可能不止想買(mǎi)一件商品,想買(mǎi)多個(gè),或許有因?yàn)槟撤N原因點(diǎn)錯(cuò)了,本來(lái)想買(mǎi)一件來(lái)著,小手不小心抖了一下,把數(shù)量錯(cuò)點(diǎn)成了三個(gè),這個(gè)時(shí)候就涉及到一個(gè)新的功能,那就是增加和減少商品的數(shù)量,今天這篇博文,小編就來(lái)和小伙伴們分享一下,如何實(shí)現(xiàn)淘寶購(gòu)物車(chē)中增加和減少商品數(shù)量的demo。
首先,我們來(lái)布局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ù)量的符號(hào)以及中間的商品數(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>
我們來(lái)看一下xml布局的頁(yè)面會(huì)是什么樣子的nie,如下圖所示:
接著,我們來(lái)編寫(xiě)Java類(lèi)里面的代碼,具體代碼如下所示:
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è)置輸入類(lèi)型為數(shù)字 edtNumber.setInputType(android.text.InputType.TYPE_CLASS_NUMBER); edtNumber.setText(String.valueOf(num)); SetViewListener(); } /** * 設(shè)置文本變化相關(guān)監(jiān)聽(tīng)事件 */ private void SetViewListener() { btAdd.setOnClickListener(new OnButtonClickListener()); btReduce.setOnClickListener(new OnButtonClickListener()); edtNumber.addTextChangedListener(new OnTextChangeListener()); } /** * 加減按鈕事件監(jiān)聽(tīng)器 * * */ 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, "請(qǐng)輸入一個(gè)大于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, "請(qǐng)輸入一個(gè)大于0的數(shù)字", Toast.LENGTH_SHORT).show(); } else { edtNumber.setText(String.valueOf(num)); } } } } } /** * EditText輸入變化事件監(jiān)聽(tīng)器 */ 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, "請(qǐng)輸入一個(gè)大于0的數(shù)字", Toast.LENGTH_SHORT).show(); } else { //設(shè)置EditText光標(biāo)位置 為文本末端 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) { } } }
最后,我們來(lái)看一下運(yùn)行效果,如下圖所示:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)購(gòu)物車(chē)功能
- Android實(shí)現(xiàn)的仿淘寶購(gòu)物車(chē)demo示例
- Android Studio實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
- Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
- Android把商品添加到購(gòu)物車(chē)的動(dòng)畫(huà)效果(貝塞爾曲線)
- Android實(shí)現(xiàn)商城購(gòu)物車(chē)功能的實(shí)例代碼
- Android中實(shí)現(xiàn)淘寶購(gòu)物車(chē)RecyclerView或LIstView的嵌套選擇的邏輯
- Android仿外賣(mài)購(gòu)物車(chē)功能
- Android實(shí)現(xiàn)添加商品到購(gòu)物車(chē)動(dòng)畫(huà)效果
- Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)
相關(guān)文章
Android 自定義精美界面包含選項(xiàng)菜單 上下文菜單及監(jiān)聽(tīng)詳解流程
這篇文章主要介紹了一個(gè)Android實(shí)例小項(xiàng)目,它包含了選項(xiàng)菜單、上下文菜單及其對(duì)應(yīng)的監(jiān)聽(tīng)事件,它很小,但這部分功能在Android開(kāi)發(fā)中很常見(jiàn),需要的朋友來(lái)看看吧2021-11-11android 開(kāi)發(fā) 文件讀寫(xiě)應(yīng)用案例分析
在Android應(yīng)用中保存文件會(huì)使用到文件讀寫(xiě)技術(shù),本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12Flutter?Widget開(kāi)發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開(kāi)發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12ListView-添加item的事件監(jiān)聽(tīng)實(shí)例
下面小編就為大家?guī)?lái)一篇ListView-添加item的事件監(jiān)聽(tīng)實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Android應(yīng)用開(kāi)發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例,例子中包括動(dòng)態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02Android自定義view實(shí)現(xiàn)左滑刪除的RecyclerView詳解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來(lái)講解RecyclerView的用法2022-11-11android-使用環(huán)信SDK開(kāi)發(fā)即時(shí)通信功能(附源碼下載)
本篇文章主要介紹了android-使用環(huán)信SDK開(kāi)發(fā)即時(shí)通信功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12Android開(kāi)發(fā)實(shí)現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)中實(shí)現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android實(shí)現(xiàn)沉浸式狀態(tài)欄
這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11