The user can quickly click on a button multiple times because the phone responds slowly or is the cause of God's horse. Then there may be a problem: like submitting data multiple times ...
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 addressed:
1. You need to define a global variable lastclicktime, which is used to record the last click Time.
2. Before each click need to make a judgment, with Lastclicktime and the current time to compare, and update the last click Time, if less than the threshold value, is invalid Click, does not trigger the event
1 Public classCommonutils {2 Private Static LongLastclicktime;3 Public Static BooleanIsfastdoubleclick () {4 LongTime =System.currenttimemillis ();5 LongTimeD = time-Lastclicktime;6 if(0 < TimeD && TimeD < 800) { 7 return true; 8 } 9Lastclicktime =Time ; Ten return false; One } A}
1 Public void OnClick (View v) { Span style= "color: #008080;" >2 if (Utils.isfastdoubleclick ()) {
3 ; 4 }else { // popup toast or dialog 6 } 7 ;}
This way, the two-click time is less than 800ms, it will not trigger the event, the specific critical time can be modified according to the requirements.
Android prevents controls from being repeatedly clicked