Android: prevents excessive clicks from causing multiple events and android events.

Source: Internet
Author: User

Android: prevents excessive clicks from causing multiple events and android events.
Android: prevents multiple events caused by excessive clicks

OnClick events are the most common events in Android development. For example, a submitButton is used to submit an order after clicking it,
The general code is as follows, wheresubmitOrder()The function jumps to the next page for processing:

// Code 0 submitButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {submitOrder ();}});

Under normal circumstances, this code is okay, but the performance of Android devices varies greatly. If you encounter a sim-card mobile phone, submitOrder () may occur () function jump page delay. In this case, the user may click it again, resulting in the function being called twice and a duplicate order BUG.
Generally, when this happens, you first think of setting submitButton as not clickable after clicking:

// Code 1 submitButton. setClickable (false); // or submitButton. setEnabled (false );

This method is also valid. However, if the submitOrder () method is not successful, you need to re-submit the order and set the submitButton to clickable. If there are too many buttons, it will be troublesome and confusing.

Solution

-----So, the next step is our comparisonEleganceMethod,~ (@ ^ _ ^ @)~ -------

CustomizeNoDoubleClickListener, Inherited fromOnClickListener:

// Code 2 public abstract class NoDoubleClickListener implements OnClickListener {public static final int MIN_CLICK_DELAY_TIME = 1000; private long lastClickTime = 0; @ Override public void onClick (View v) {long currentTime = Calendar. getInstance (). getTimeInMillis (); if (currentTime-lastClickTime> MIN_CLICK_DELAY_TIME) {lastClickTime = currentTime; onNoDoubleClick (v );}}}

Use method -- replace OnClickListener with NoDoubleClickListener when setting a click event for submitButton, and use onNoDoubleClick instead of onClick as follows:

// Code 3 submitButton. setOnClickListener (new NoDoubleClickListener () {@ Override public void onNoDoubleClick (View v) {submitOrder ();}});
Principle:

Very simple. See the code ......
OnNoDoubleClick is used instead of onClick to process specific operations. In The onClick method, add a judgment: After receiving the click event, first judge the time. If the time is not long from the last processing operation, MIN_CLICK_DELAY_TIME, ignore-This prevents repeated events caused by incorrect continuous clicks.

MIN_CLICK_DELAY_TIME is adjustable.

Advantages

The advantage is that you only need to replace NoDoubleClickListener with OnClickListener and onNoDoubleClick instead of onClick without changing the logic of the original code. You do not need to change the structure of the Code (** compare the code 0 with the code above ** 3). You do not need to worry about the additional Judgment logic for processing the changed button status, you only need to focus on the business logic, concise and elegant ~

This article synced to my independent blog post: http://www.barryzhang.com/archives/410

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.