Android Custom Product Purchase quantity add and subtract control _android

Source: Internet
Author: User

In the purchase of goods, you can customize the number control, to determine the actual number of goods purchased, how to implement this control, please refer to the following:

1. Customize the requirements of the number plus and minus controls

Create Module-numberaddsubview

A_ input can only be a number, and can not enter through the keyboard

B_ the number by adding and subtraction buttons

C_ Monitor plus minus button

The D_ array has a minimum and maximum limit

E_ Custom Properties

2. Provide the interface, let the outside monitor the change of the number

1_ Settings interface

 @Override public void OnClick (View v) {if (V.getid () = = R.id.btn_sub) {//toast.maketext (M
  Context, "minus", Toast.length_short). Show ();
  Subnum ();
  if (onbuttonclicklistenter!= null) {Onbuttonclicklistenter.onbuttonsubclick (V, value);
  } else if (v.getid () = = R.id.btn_add) {//toast.maketext (Mcontext, add, Toast.length_short). Show ();
  Addnum ();
  if (onbuttonclicklistenter!= null) {Onbuttonclicklistenter.onbuttonaddclick (V, value); }} public interface Onbuttonclicklistenter {/** * callback This method when the Add button is clicked * * @param view * @param value/PU
 
 Blic void Onbuttonaddclick (view view, int value);
/** * Callback This method when the reduction button is clicked * * @param view * @param value */public void Onbuttonsubclick (view view, int value);
 
Private Onbuttonclicklistenter Onbuttonclicklistenter; public void Setonbuttonclicklistenter (Onbuttonclicklistenter onbuttonclicklistenter) {this.onbuttonclicklistenter =
Onbuttonclicklistenter; }

2_ Monitoring Changes

public class Mainactivity extends Appcompatactivity {
 
 private numberaddsubview nb_addsub_view;
 
 @Override
 protected void onCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);
  Nb_addsub_view = (Numberaddsubview) Findviewbyid (R.id.nb_addsub_view);
 
  Nb_addsub_view.setonbuttonclicklistenter (New Numberaddsubview.onbuttonclicklistenter () {
   @Override
   public void Onbuttonaddclick (view view, int value) {
    toast.maketext (mainactivity.this, "AddClick vaule==" +value, Toast.length_short). Show ();
 
   @Override public
   void Onbuttonsubclick (view view, int value) {
    toast.maketext (mainactivity.this, "Subclick vaule== "+value,toast.length_short"). Show ();}}

GitHub Complete code: Https://github.com/ganchuanpu/NumberAddSubView

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.