In development often encounter such a situation, a button click will pop up toast or dialog, if you click quickly and repeatedly, then toast will appear repeatedly. And the effect we want is to have the click of a certain time to take effect once, or to say this quick and repetitive click for invalid clicks.
The solution to the following ideas:
1. You need to define a global variable lastclicktime to record the last click Time.
2. Before each click need to judge, with Lastclicktime and the current time to compare, and update the last click Time, if less than the critical value, then calculate invalid click, not trigger events
Here's a look at the code:
Tool class:
public class Commonutils {
private static long lastclicktime;
public static Boolean Isfastdoubleclick () {
Long time = System.currenttimemillis ();
Long TimeD = Time-lastclicktime;
if (0 < TimeD && TimeD < 800) {
return true;
}
Lastclicktime = time;
return false;
}
}
Control Click Processing:
public void OnClick (View v) {
if (Utils.isfastdoubleclick ()) {
Return
}else{
Pop Toast or Dialog
}
}
In this way, two clicks a time interval of less than 800ms, will not trigger events, the specific critical time can be modified according to the needs of their own.