Android: Long-pressed continuous response Button, android response button

Source: Internet
Author: User

Android: Long-pressed continuous response Button, android 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 (conte Xt, 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;});}/*** the thread will start on time */ 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, make the listening Event Response in the main thread */private static class MyHandler extends Handler {private WeakReference <LongClickButton> ref; MyHandler (LongClickButton Ton) {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 ));}});

3. In many cases, long-pressed events are the same as normal click events (that is, long-pressed events are equivalent to Continuous quick clicks ). In this case, the Custom Button can be more concise: that is, the normal OnClickListener can be called continuously for a long time.

Public class LongClickButton2 extends Button {/*** interval (MS) */private long intervalTime = 50; private MyHandler handler; public LongClickButton2 (Context context) {super (context ); init ();} public LongClickButton2 (Context context, AttributeSet attrs) {super (context, attrs); init ();} public LongClickButton2 (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleA Ttr); 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 (LongClickButton2.this. isPre Ssed () {num ++; if (num % 5 = 0) {handler. sendEmptyMessage (1);} SystemClock. sleep (intervalTime/5); }}/ *** use handler, make the listening Event Response in the main thread */private static class MyHandler extends Handler {private WeakReference <LongClickButton2> ref; MyHandler (LongClickButton2 button) {ref = new WeakReference <> (button) ;}@ Override public void handleMessage (Message msg) {super. handleMessage (msg); LongClick Button2 button = ref. get (); if (button! = Null) {// call the common click event button. Invoke mclick () ;}} public void setIntervalTime (long intervalTime) {this. intervalTime = intervalTime ;}}



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.