Android prevents controls from being repeatedly clicked

Source: Internet
Author: User

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.

Related Article

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.