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