Android prevents controls from being repeatedly clicked

Source: Internet
Author: User

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

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.