button click Frequency Control in Android

Source: Internet
Author: User

   Public classUtils {Private Static LongLastclicktime;  Public StaticBoolean Isfastdoubleclick () {LongTime =System.currenttimemillis (); LongTimeD = time-Lastclicktime; if(0< TimeD && TimeD < -) {//the button is not valid in 500 milliseconds, so you can control the Quick click and adjust the frequency yourself .              return true; } lastclicktime=Time ; return false; }     }                                                               //Listener Button Call method   Public voidOnClick (View v) {if(Utils.isfastdoubleclick ()) {return; }     } 

-----------------------------------------------------------------you can test it yourself, absolutely not.

Write a class

Package Com.example.teststyle;import Android.util.Log; Public classbuttonutil{Private Static LongLastclicktime =0; Private Static LongDIFF = the; Private Static intLastbuttonid =-1; /** * Determine the interval of two clicks, if less than 1000, is considered to be multiple invalid clicks * @return*/  Public StaticBoolean Isfastdoubleclick () {returnIsfastdoubleclick (-1, DIFF); } /** * Determine the interval of two clicks, if less than 1000, is considered to be multiple invalid clicks * @return*/  Public StaticBoolean Isfastdoubleclick (intButtonID) {  returnIsfastdoubleclick (Buttonid,diff);} /** * To determine the interval of two clicks, if it is less than diff, it is considered multiple invalid click * @param diff * @return*/  Public StaticBoolean Isfastdoubleclick (intButtonID,Longdiff) {  LongTime =System.currenttimemillis (); LongTimeD = time-Lastclicktime; LOG.V ("xxxx","Lastbuttonid ="+lastbuttonid+"ButtonID ="+ButtonID); if(Lastbuttonid = = ButtonID && lastclicktime>0&& TimeD <diff) {LOG.V ("Isfastdoubleclick","button trigger multiple times in a short time"); return true; } lastclicktime=Time ; Lastbuttonid=ButtonID; return false; }}

and then call

 Public voidOnClick (View v) {//TODO auto-generated Method Stub    if(Buttonutil.isfastdoubleclick (V.getid ())) {//control here, time to control.LOG.E ("-----> Show Click Time and Start time","Start Time"); Toast.maketext (Getapplicationcontext (),"Show Content", Toast.length_short). Show (); return    }   //start writing your own business logic   ...........   }

button click Frequency Control in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.