How does Android prevent multiple click events

Problem description

I'm afraid everyone will encounter such a problem. A click event is triggered many times, resulting in the same content being submitted many times, or multiple pages popping up

Onclick event is the most common event in Android development. For example, if a submitbutton is used to submit an order after clicking, the general code is as follows, in which the submitorder() function will jump to the next page for processing:

Normally, there is no problem with this code, but the performance of Android devices varies greatly. If you encounter a mobile phone with a card comparison, there may be a delay in the page Jump of the submitorder() function. In case of this phenomenon, the user is likely to click again, resulting in the function being called twice and the bug of repeated orders.

Generally, when you encounter this phenomenon, the first thing you think of is to set the submitbutton to non clickable after clicking:

This method is also effective, but if the submitorder () method fails, you need to repeatedly set the submitbutton to clickable state when you need to submit the order again. If there are many similar buttons, it will be more troublesome and chaotic.

programme

Customize a nodoubleclicklistener, which inherits from onclicklistener:

Usage method: when setting the click event for the submitbutton, use nodoubleclicklistener instead of onclicklistener, and implement the method onnodoubleclick instead of onclick, as follows:

Principle:

Very simple, see the code

It is to use onnodoubleclick instead of onclick to process specific operations. Add a judgment in the onclick method: after receiving the click event, judge the time first. If it is less than min from the last processing operation_ CLICK_ DELAY_ Time, ignore - that is, it can prevent repeated events caused by continuous clicks of misoperation.

MIN_ CLICK_ DELAY_ Time is adjustable.

advantage

The advantage is that without changing the logic of the original code, only two substitutions are required: nodoubleclicklistener instead of onclicklistener and onnodoubleclick instead of onclick. The structure of the code doesn't need to be changed (* * compare the above code 0 with code * * 3), and you don't need to care about the additional judgment logic of changing the state of the button. You just need to pay attention to the business logic, which is concise and elegant~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>