Android to realize Taobao shopping cart increase and reduce the number of merchandise function Demo sample _android

Source: Internet
Author: User
Tags event listener gettext sqlite database

This article illustrates the Android implementation of the Taobao shopping cart to increase and reduce the number of merchandise functions. Share to everyone for your reference, specific as follows:

In a previous example of Android's imitation Taobao shopping cart demo, small part of the simple introduction of how to use ListView to implement the shopping cart, but simply to achieve the function of the list, along with a new problem, when buying goods, we may not only want to buy a product, want to buy multiple , maybe it's something wrong for some reason, originally wanted to buy a, small hands accidentally shook a bit, the number of wrong points into three, this time involves a new function, that is, to increase and reduce the number of goods, today this blog, small knitting to and small partners to share, How to achieve Taobao shopping cart to increase and reduce the number of products demo.

First, we'll lay out the XML file, as shown in the following code:

<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 "> <!--overall layout, including the symbols for increasing and reducing the number of items and the number of commodities--> <linearlayout android:layout_width=" Fill_parent " android:layout_height= "wrap_content" android:orientation= "Horizontal" > <!--reduce the layout of the number of items--> <bu
      Tton android:id= "@+id/addbt" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" 
      Android:textcolor= "#0157D3" android:text= "-" > </Button> <!--The layout of the quantity of merchandise--> <edittext Android:id= "@+id/edt" android:text= "0" aNdroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" > </EditText> <!--increase the number of goods The layout--> <button android:id= "@+id/subbt" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:textcolor= "#0157D3" android:text= "+" > </Button> <!--display the layout of the quantity of goods --> <textview android:id= "@+id/ttt" android:layout_width= "Wrap_content" android:layout_height=

 "Wrap_content" > </TextView> </LinearLayout> </RelativeLayout>

Let's take a look at what the page of the XML layout will look like NIE, as shown in the following illustration:

Next, we'll write the code inside the Java class, as shown in the following code:

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;
    Quantity 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 ("-");
    Set the input type to digital edtnumber.setinputtype (Android.text.InputType.TYPE_CLASS_NUMBER);
    Edtnumber.settext (string.valueof (num));
  Setviewlistener ();
}
  /**   * Set text change related listening event/private void Setviewlistener () {Btadd.setonclicklistener (New Onbuttonclicklistener ());
    Btreduce.setonclicklistener (New Onbuttonclicklistener ());
  Edtnumber.addtextchangedlistener (New Ontextchangelistener ()); /** * Add and Subtract button event listener * */class Onbuttonclicklistener implements Onclicklistener {@Override Publ
      IC 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)//First add, then judge {
            num--;
          Toast.maketext (Shoppingcartitemactivity.this, "Please enter a number greater than 0", Toast.length_short). Show ();
          else {edtnumber.settext (string.valueof (num)); } else if (V.gettag (). Equals ("+")) {if (--num < 0)//minus First,Judge again {num++;
          Toast.maketext (Shoppingcartitemactivity.this, "Please enter a number greater than 0", Toast.length_short). Show ();
          else {edtnumber.settext (string.valueof (num));
    /** * EditText Input Change Event listener */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, please enter a number greater than 0), Toast.le
        Ngth_short). Show (); else {//set EDITTEXT cursor position to text end 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) {}}

Finally, let's look at the effects of the operation, as shown in the following illustration:

More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View Overview", "The activity of Android programming skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Development Primer and Advanced tutorials, Android Resource operations tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.