Problem description
I'm afraid everyone will encounter such a problem. A click event is triggered multiple times, resulting in the same content being submitted multiple times, or multiple pages pop up...
The onClick event is the most common event in Android development. For example, a submitButton function is to submit an order after clicking. The general code is as follows, where the submitOrder() function will jump to the next page for processing:
<code class="hljs" java=""> //Code 0 (new OnClickListener() { @Override public void onClick(View v) { submitOrder(); } }); </code>
Under normal circumstances, there is no problem with this code, but the performance of Android devices varies greatly. If you encounter a relatively stuck phone, the submitOrder() function may cause delay in jumping to the page. When encountering this phenomenon, the user is likely to click once again, resulting in the function being called twice and a bug of repeated orders appears.
Generally speaking, when encountering this phenomenon, the first thing that comes to mind is to set submitButton to be non-clickable after clicking:
<code class="hljs" cs=""> //Code 1 (false); //or (false); </code>
This method is also effective, but if the submitOrder() method is not successful, you need to repeatedly set the submitButton to a clickable state when you need to submit the order again. If there are many similar buttons, it will appear more troublesome and confusing.
plan
Customize a NoDoubleClickListener, inherited from OnClickListener:
<code class="hljs" java=""> //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 = ().getTimeInMillis(); if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) { lastClickTime = currentTime; onNoDoubleClick(v); } } }</code>
How to use - Use NoDoubleClickListener instead of OnClickListener when setting a click event for submitButton, and implement the method onNoDoubleClick instead of onClick, like this:
<code class="hljs" java=""> //Code 3 (new NoDoubleClickListener() { @Override public void onNoDoubleClick(View v) { submitOrder(); } }); </code>
principle:
It's very simple, see the code...
It is to use onNoDoubleClick instead of onClick to handle specific operations. Add a judgment to the onClick method: After receiving the click event, first judge the time. If the MIN_CLICK_DELAY_TIME is insufficient from the last processing operation, ignore it - that is, it can prevent the misoperated continuous clicks to cause duplicate events.
MIN_CLICK_DELAY_TIME adjustable.
Advantages
The advantage is that there is no need to change the logic of the original code, only two replacements are needed: NoDoubleClickListener instead of OnClickListener, and onNoDoubleClick instead of onClick. The structure of the code does not need to be changed (**Compare the above code 0 and code **3), and you do not need to care about handling the state of the button to change the extra judgment logic. You only need to pay attention to the business logic, which is simple and elegant~