Android Prevent button Multiple Click Summary

Source: Internet
Author: User

    Long Lastclick;    public void OnClick (View v) {          ///greater than one second square through          if (System.currenttimemillis ()-Lastclick <=)          {              Showtoast ("What's the point!!!");              return;          }          Lastclick = System.currenttimemillis ();      }  

A common way of writing, recording the time of the two click events before and after, and then judging whether a difference is within a given limittime, if it is not clicked,

Otherwise, you can click.


This way has shortcomings, multiple button, fast switch click, the control effect is not very good, so I try to use the Android handler to control button click event.


//new            A Handler for message processing Handler Handler = new Handler () {@Override public void handlemessage (message msg) {            Super.handlemessage (msg);            if (msg.what = = Goto_dialog) {//Call button click event Handling Method Gotodialog Gotodialog ((Integer) msg.obj); }        }    };       Clears the message handler.removemessages (Goto_dialog) before @Override public void OnClick (View v) {//handler message is sent;        Binds a msg, which is the ID of the button to be needed next, Message msg = Message.obtain ();        Msg.what = Goto_dialog;        Msg.obj = V.getid ();    Send Message interval 1 sec handler.sendmessagedelayed (msg, 1000); } private void Gotodialog (int id) {//multiple botton switch (ID) {case r.id.btn_back:.           ... break;      Case R.id.btn_sumbit: ... break; }
With the above handler processing, you no longer have to worry about switching between multiple buttons.

Note: Remember to destroy the handler after you run out of


Android Prevent button Multiple Click Summary

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.