Android prevents controls from being repeatedly clicked

Source: Internet
Author: User

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

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.