In the e-commerce app's shopping cart page, there is often a need for a continuous increase or decrease in the number of items in the shopping cart as long as the "+" button or the "-" button is pressed.
The purpose of this example is to achieve this effect. Preview Map:
1. Customize the button.
/** * Long Press the button with continuous response * Created by admin on 15-6-1. */public class Longclickbutton extends Button {/** * long pressed for continuous response listening, long time will call the method in the interface multiple times until the end of long press/private Longclickr Epeatlistener Repeatlistener; /** * Interval Time (MS) */private long intervaltime; Private MyHandler handler; Public Longclickbutton (Context context) {super (context); Init (); } public Longclickbutton (context context, AttributeSet Attrs) {Super (context, attrs); Init (); } public Longclickbutton (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyle ATTR); Init (); }/** * Initialize monitor */private void init () {handler = new MyHandler (this); Setonlongclicklistener (New Onlongclicklistener () {@Override public boolean onlongclick (View v) { New Thread (New Longclickthread ()). Start (); return true; } }); }/** * Long and on time, the thread will start */Private class Longclickthread implements Runnable {private int num; @Override public void Run () {while (LongClickButton.this.isPressed ()) {num++; if (num% 5 = = 0) {handler.sendemptymessage (1); } systemclock.sleep (INTERVALTIME/5); }}}/** * Handler to make the listener's event response in the main thread */private static class MyHandler extends Handler {p Rivate weakreference<longclickbutton> ref; MyHandler (Longclickbutton button) {ref = new weakreference<> (button); } @Override public void Handlemessage (Message msg) {super.handlemessage (msg); Longclickbutton button = Ref.get (); if (Button! = NULL && Button.repeatlistener! = null) {button.repeatListener.repeatAction (); }}}/** * Sets the listening and interval time for the continuous response, and will call the method in the interface multiple times in a long time until the end of long press * * @param listener Monitor * @param intervaltime interval (ms) */public void Setlongclickrepeatlistener (LONGCL Ickrepeatlistener Listener, Long intervaltime) {This.repeatlistener = listener; This.intervaltime = IntervalTime; }/** * Set long press for continuous response listening (using the default interval time of 100ms), long time will call the method in the interface multiple times until the end of Long press * * @param listener monitoring */public void Setl Ongclickrepeatlistener (Longclickrepeatlistener listener) {Setlongclickrepeatlistener (listener, 100); } public interface Longclickrepeatlistener {void repeataction (); }}2. Call in activity:
Longclickbutton buttonsub = (Longclickbutton) Findviewbyid (R.id.long_click_button1); Longclickbutton Buttonadd = (Longclickbutton) Findviewbyid (R.id.long_click_button2); Final TextView Numbertv = (TextView) Findviewbyid (R.id.main_number); Continuous minus Buttonsub.setlongclickrepeatlistener (new Longclickbutton.longclickrepeatlistener () {@Override public void Repeataction () {Numbertv.settext (string.valueof (Integer.parseint () (Numbertv.gettext (). String ())-1); }}, 50); Continuous plus Buttonadd.setlongclickrepeatlistener (new Longclickbutton.longclickrepeatlistener () {@Override public void Repeataction () {Numbertv.settext (string.valueof (Integer.parseint () (Numbertv.gettext (). String ()) + 1)); }}, 50); Minus 1 Buttonsub.setonclicklistener (new View.onclicklistener () {@Override public void OnClick ( View v) {nUmbertv.settext (String.valueof (Integer.parseint (Numbertv.gettext (). toString ())-1)); } }); Add 1 Buttonadd.setonclicklistener (new View.onclicklistener () {@Override public void OnClick ( View v) {numbertv.settext (string.valueof (Integer.parseint () (Numbertv.gettext (). toString ()) + 1); } });
Android: Long-pressed button with continuous response