Android double-click event processing

Source: Internet
Author: User

Share with you the following principles:

Activity: Main

Package COM. my. test; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. motionevent; <br/> Import android. view. view; <br/> Import android. view. gesturedetector. ongesturelistener; <br/> Import android. view. view. ontouchlistener; <br/> Import android. widget. button; <br/> Import android. widget. viewflipper; </P> <p> public class main extends activity implements ontouchlistener {<br/> // Private viewflipper mflipper; <br/> // Private layoutinflater factory; <br/> // Private view; <br/> private button BTN; <br/> private long firtick = 0l; <br/> private long sectick = 0l; <br/> private long distance = 0l; <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> _ Init (); <br/>}< br/> void _ Init () {<br/> // factory = layoutinflater. from (this); <br/> // view = factory. inflate (R. layout. other, null); <br/> BTN = (button) findviewbyid (R. id. b1); <br/> BTN. setontouchlistener (this); <br/>}</P> <p> @ override <br/> Public Boolean ontouch (view V, motionevent event) {<br/> // todo auto-generated method stub <br/> // determine the double-click event by time difference. You can split the double-click event into two consecutive Independent Events: 1. Select; 2. Execute <br/> switch (event. getaction () {<br/> case motionevent. action_down: <br/> switch (v. GETID () {<br/> case R. id. b1: <br/> If (firtick = 0l) {<br/> firtick = system. currenttimemillis (); // time of the previous click <br/> system. out. println ("Touch"); <br/>} else if (sectick = 0l) {// The next click time <br/> sectick = system. currenttimemillis (); <br/> distance = sectick-firtick; <br/> If (distance> 0l & distance <500l) {<br/> // the time range is set freely. If it is set to true, click continuously. <br/> system. out. println ("double"); <br/> firtick = 0l; <br/> sectick = 0l; <br/>} else {<br/> // click not consecutive <br/> firtick = system. currenttimemillis (); // obtain the time of the previous Click again <br/> sectick = 0l; <br/> system. out. println ("Touch"); <br/>}</P> <p >}< br/> break; <br/> default: <br/> break; <br/>}< br/> break; </P> <p> default: <br/> break; <br/>}< br/> return false; <br/>}< br/>}

 

There is only one button in Main. XML with the ID B1

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical" <br/> <button Android: id = "@ + ID/B1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "test"/> <br/> </linearlayout>

Comments

 

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.