Android: Prevent excessive clicks causing multiple events

Source: Internet
Author: User

Android: Prevents multiple event problems caused by too fast clicks

The onclick event is the most common event in Android development. For example, a Submitbutton, the function is to click after the submission of an order,
The general code is as follows, where the submitOrder() function jumps to the next page for processing:

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

Under normal circumstances this code is not a problem, but the Android device model performance, and so on, if you encounter the comparison card phone, there may be submitorder () function jump page delay phenomenon. When this happens, the user is likely to click again, causing the function to be called two times, with a bug that duplicates the order.
Generally, when you encounter this phenomenon, the first thing you will think about is to set the Submitbutton to not click after clicking:

    //代码1    submitButton.setClickable(false);    //或者       

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.

Scheme

————-So, next is the 优雅 way we look at the comparison, ~ (@ ^_^ @) ~ ———————

Custom one NoDoubleClickListener , inherited from OnClickListener :

    //代码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 the method--to Submitbutton set the Click event with Nodoubleclicklistener instead of Onclicklistener, and implement the method Onnodoubleclick instead of the onclick, like this:

    //代码3    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 deal with the specific operation, in the OnClick method to add a judgment: after receiving the Click event, the first to determine the time, if the distance from the last processing operation is not enough min_click_delay_time, ignored-- A continuous Click that prevents accidental operation causes a recurring event.

Min_click_delay_time adjustable.

Advantage

The advantage is that instead of changing the logic of the original code, only two substitutions 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), do not need to care about the state of the Change button to the additional judgment logic, only need to focus on business logic, simple and elegant ~

This article was posted on my standalone blog: http://www.barryzhang.com/archives/410

Android: Prevent excessive clicks causing multiple events

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.