Android prevents controls from being repeatedly clicked

Source: Internet
Author: User

In development often encounter such a situation, a button click will pop up toast or dialog, if you click quickly and repeatedly, then toast will appear repeatedly. And the effect we want is to have the click of a certain time to take effect once, or to say this quick and repetitive click for invalid clicks.

The solution to the following ideas:

1. You need to define a global variable lastclicktime to record the last click Time.

2. Before each click need to judge, with Lastclicktime and the current time to compare, and update the last click Time, if less than the critical value, then calculate invalid click, not trigger events

Here's a look at the code:

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 Processing:

public void OnClick (View v) {

if (Utils.isfastdoubleclick ()) {

Return

}else{

Pop Toast or Dialog

}

}

In this way, two clicks a time interval of less than 800ms, will not trigger events, the specific critical time can be modified according to the needs of their own.

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.