This is often the case in development, when a button is clicked and a toast or dialog pops up, and if you click quickly and repeatedly, the toast will appear repeatedly. And the effect we want is that a click in a certain amount of time only takes effect once, or that this fast and repetitive click is invalid click. The following ideas are resolved: 1. You need to define a global variable lastclicktime, which is used to record the last click Time. 2. Each click before the need to judge, with Lastclicktime and the current time to compare, and update the last click Time, if less than the threshold value, then invalid click, do not trigger the event below please see Code: Tool Class: [Code]java Code:
public class Commonutils {private static long lastclicktime; public static Boolean Isfastdoubleclick () {Long time = System.currenttimemillis (); Long TimeD = Time-lastclicktime; if (0 < TimeD && TimeD < +) {return true; } lastclicktime = time; return false; }} control click Processing: [Code]java Code:
public void OnClick (View v) {if (Utils.isfas Tdoubleclick ()) {return;
}else{//Popup toast or dialog} } this way, the two-click time is less than 800ms, the event will not be triggered, the specific critical time can be modified on demand. |
Android prevents controls from being repeatedly clicked