Android: Long-pressed continuous response Button
On the shopping cart page of E-commerce apps, there is often the need to increase or decrease the number of items in the shopping cart continuously when you press the "+" button or "-" button.
The purpose of this example is to achieve this effect. Preview:
1. Custom Button.
/*** Long-pressed Button * Created by admin on 15-6-1. */public class LongClickButton extends Button {/*** long-pressed listener for continuous response. The method in this interface will be called multiple times on a long time until long-pressed ends */private LongClickRepeatListener repeatListener; /*** time interval (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, defStyleAttr); init ();}/*** initialize listener */private void init () {handler = new MyHandler (this); setOnLongClickListener (new OnLongClickListener () {@ Override public boolean onLongClick (View v) {new Thread (new LongClickThread ()). start (); return true;});}/*** long time, this 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) ;}}/ *** use handler to make the listening Event Response run in the main thread */private static class MyHandler extends Handler {private WeakReference
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 listener and interval for a long response, long time will call the method in this interface multiple times until long press End ** @ param listener listen * @ param intervalTime interval (MS) */public void setLongClickRepeatListener (LongClickRepeatListener listener, long intervalTime) {this. repeatListener = listener; this. intervalTime = intervalTime;}/*** sets the listener with a long response (the default interval is 100 ms ), the method in this interface will be called multiple times until the end of the long press ** @ param listener */public void setLongClickRepeatListener (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); // consecutive subtraction of buttonSub. setLongClickRepeatListener (new LongClickButton. longClickRepeatListener () {@ Override public void repeatAction () {numberTV. setText (String. valueOf (Integer. parseInt (numberTV. getText (). toString ()-1) ;}}, 50); // Add buttonAdd consecutively. setLongClickRepeatListener (new LongClickButton. longClickRepeatListener () {@ Override public void repeatAction () {numberTV. setText (String. valueOf (Integer. parseInt (numberTV. getText (). toString () + 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 ));}});