Double-click a view on Android

Source: Internet
Author: User

Android does not have double-click events by default. I don't know why ~~.. Not to mention, go directlyCode.

First, add a double-click event interface:

 
Public interface ondoubleclicklistener {public void onsingleclick (view V); Public void ondoubleclick (view V );}
 
 

Then add a method:

/*** Register a double-click event * from a netizen and add handler for processing. If this is not added, it will cause errors such as thread security.
*/Public static void registerdoubleclicklistener (view, final ondoubleclicklistener listener) {If (listener = NULL) return; view. setonclicklistener (new view. onclicklistener () {Private Static final int double_click_time = 350; // double-click the interval of 350 milliseconds private Boolean waitdouble = true; private handler = new handler () {@ overridepublic void handlemessage (Message MSG) {listener. onsingleclick (View) MSG. OBJ) ;}}; // wait for double-click public void onclick (final view v) {If (waitdouble) {waitdouble = false; // run the double-click event new thread () {public void run () {try {thread. sleep (double_click_time);} catch (interruptedexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // wait for the double-click time; otherwise, click the event if (! Waitdouble) {// If the waiting event or pre-execution double-click status is passed, it is deemed that waitdouble = true is clicked; message MSG = handler. obtainmessage (); MSG. OBJ = V; handler. sendmessage (MSG );}}}. start () ;}else {waitdouble = true; listener. ondoubleclick (V); // double-click it }}});}

Last registration event:

 
Imageview = (imageview) findviewbyid (R. Id. imageview1 );
 
Registerdoubleclicklistener (imageview, this );

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.