Android solution to prevent multiple response events caused by too fast clicking

Recently, there are some small problems in the development process. For example, a BTN click user may only click once, but the background responds many times. Such problems as the submission of some forms are difficult. Of course, there are many solutions to this problem: for example, customize the BTN with a single click, or set the button to be non clickable when clicking, Or build a timer. These are solutions. As for how to use it, it depends on the programmer's coding style. First, let's look at the specific problems:

Onclick event is the most common event in Android development. For example, if a button is used to submit an order when clicked, the general code is as follows:

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.

Here is my method:

Customize a checkdoubleclicklistener, which inherits from onclicklistener. Oncheckdoubleclick is a customized interface. See the code for details:

CheckDoubleClickListener.Java

OnCheckDoubleClick.java

usage method:

When setting click events for BTN, use checkdoubleclicklistener instead of onclicklistener, and implement the method oncheckdoubleclick instead of onclick, as follows:

Principle:

It is to use oncheckdoubleclick 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.

Advantages:

The advantage is that without changing the logic of the original code, only two substitutions are needed: checkdoubleclicklistener instead of onclicklistener and oncheckdoubleclick instead of onclick. The structure of the code doesn't need to be changed. We don't need to care about the additional judgment logic of processing and changing the BTN state. We just need to pay attention to the business logic. Has the code become very concise? Come on.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>