Android prevents over-clicking to cause multiple events one line of code to fix, especially simple, can be reused

Source: Internet
Author: User

The onclick event is the most common event in Android development, and normally this code is no problem.
But Android devices, such as the performance of a variety of models, if you hit the comparison card phone,
There is a possibility of a delay in the SubmitOrder () function jump page. When this happens, the user is likely to click again,
This causes the function to be called two times and a bug with duplicate orders appears.


Workaround
Scenario I: (not recommended)
Generally, when you encounter this phenomenon, the first thing you will think about is to set the Submitbutton to not click after clicking:
This method is also valid, but if the SubmitOrder () method does not succeed, you will need to repeat the Submitbutton setting to be clickable when you need to submit the order again.
If a similar button is more than a few, it is more troublesome and confusing.

Programme II
Customize a Nodoubleclicklistener, inherited from Onclicklistener:
Public abstract class Nodoubleclicklistener implements Onclicklistener {public static final int min_click_delay_time = 100 0;//set here cannot be longer than how long the private long lastclicktime = 0;protected abstract void Onnodoubleclick (View v); @Overridepublic void OnClick (View v) {Long currenttime = Calendar.getinstance (). Gettimeinmillis (); if (Currenttime-lastclicktime > Min_ Click_delay_time) {lastclicktime = Currenttime;onnodoubleclick (v);}}   }



To Submitbutton set the Click event with Nodoubleclicklistener instead of Onclicklistener, and implement the method Onnodoubleclick instead of the onclick, like this:

Submitbutton.setonclicklistener (New Nodoubleclicklistener () {            @Override public            void Onnodoubleclick (View v) {                submitorder ();            }        });




Principle:
Very simple, see the code ...
is to use Onnodoubleclick instead of the onclick to handle the specific operation, in the OnClick method to add a judgment: after receiving the Click event,
Judge the time first, if the distance from the last processing operation is insufficient min_click_delay_time, it is ignored-that is, to prevent the continuous click of the misoperation caused by repeated events.
Min_click_delay_time adjustable.


Advantage
The advantage is that instead of changing the logic of the original code, only two replacements are needed: Nodoubleclicklistener instead of Onclicklistener,
Onnodoubleclick instead of onclick. The structure of the code does not need to be changed (* * Compare the above code 0 with code **3),
No need to worry about handling changes to the state of the button these additional judgment logic, just focus on business logic, simple and elegant ~









Android prevents over-clicking to cause multiple events one line of code to fix, especially simple, can be reused

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.