This is often the case in development. After a button is clicked, the Toast or Dialog will pop up. If you click it again quickly, the Toast will appear again. the effect we want is that the click takes effect only once in a certain period of time, or this fast and repeated click is invalid.
The solution is as follows:
1. You need to define a global variable lastClickTime to record the last click Time.
2. Make a judgment before each click. Use lastClickTime to compare with the current time, and update the last click time. If the value is smaller than the critical value, the click is invalid and no event is triggered.
See the code below:
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 handling:
Public void onClick (View v ){
If (Utils. isFastDoubleClick ()){
Return;
} Else {
// Pop up Toast or Dialog
}
}
In this way, the two clicks are less than 800 ms apart, and the event will not be triggered. The specific critical time can be modified as needed.