Android Basic Events

Source: Internet
Author: User
<span id="Label3"></p>first, Android Basic Events<br><p><p>1.onTouchEvent Touch Event (gesturedetector gesture Action Class)</p></p>2.onKeyDown Keyboard Press<br><p><p>3.onKeyLongPress Long press event (keyboard pressed event Length)</p></p><p><p>4.onKeyUp Keyboard Release</p></p><p><p>5.onKeyMultiple Keyboard Repeat Click</p></p><p><p>6.onBackPressed Return Event</p></p>Note: keyboard events can get static member variables through the KeyEvent class (for example: keyevent.keycode_back return key listener to determine if the values of KeyCode and Keyevent.keycode_back are equal); Screen events by Event.getaction () get actions with Motionevent.action_down and motionevent.action_up to determine whether to press or release, to get the x and y coordinates when pressed, get the x and y coordinates when released, By comparison you can judge the direction of sliding, and if it is a complex gesture, it will be done by the Gesturedetector class (personal advice to use this class).<br><p><p><br></p></p><p><p>Ii. the difference between ontouchevent and ontouch? (other Same)<br></p></p><p><p>Use of 1.onTouch:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Tactivity extends Activity implements Ontouchlistener {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated Method stubsuper.oncreate (savedinstancestate); Setcontentview ( r.layout.main); TextView TV = (TextView) Findviewbyid (r.id.tv); tv.setontouchlistener (this);} OnTouch using @overridepublic boolean onTouch (View v, motionevent event) {//TODO auto-generated method Stubswitch (EVENT.G Etaction ()) {case motionevent.action_down://press break;case motionevent.action_up://release break;} Return false;}}</pre></pre><p><p></p></p><p><p>Use of 2.onTouchEvent:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Tactivity extends Activity{@Overridepublic boolean ontouchevent (motionevent event) {//TODO auto-generated m Ethod Stubreturn super.ontouchevent (event);}}</pre></pre><p><p></p></p><p><p>3.Android related source:<br></p></p><pre name="code" class="java"><pre name="code" class="java">public boolean dispatchtouchevent (motionevent ev) { if (ev.getaction () = = Motionevent.action_down) { Onuserinteraction (); } If (getWindow (). superdispatchtouchevent (ev)) { return true; } return ontouchevent (ev);}</pre></pre>Summary: you can see that the Ontouchlistener interface priority is higher than ontouchevent, if the Ontouch method in Ontouchlistener returns true, then Ontouchevent is not receiving the Message. Because the Button's PerformClick is implemented using ontouchevent, if Ontouchevent is not called, then the Button's click event is not responding.<br><br>three, Gesturedetector gesture Operation class parsing<br><p><p></p></p><p><p>Gesturedetector This class provides two interfaces externally: ongesturelistener,ondoubletaplistener, and an internal class Simpleongesturelistener.<br></p></p><p>1. Implement Interface: Ongesturelistener<br>Trigger when A.ondown () Touch down<br>B.onshowpress () Touch does not trigger when sliding (compared to Ondown,onlongpress) Ondown as long as touch down must be triggered immediately. and touchdown after a while not sliding first triggered onshowpress and then onlongpress. So after touchdown has not slipped, ondown->onshowpress->onlongpress this sequence trigger.<br>C.onsingletapup () above both functions are not sliding after touch down (onscroll), not long pressed (onlongpress), and then triggered by Touchup.<br>Click on the very fast (do not Swipe) touchup:ondown->onsingletapup->onsingletapconfirmed<br>Click a little bit slower (don't Swipe) touchup:ondown->onshowpress->onsingletapup->onsingletapconfirmed<br>D.onscroll () Drag the event on the Screen. Whether you drag a view by hand, or if you scroll with a throw, it triggers multiple times, and this method triggers a throw when the Action_move action occurs: the finger touches the screen, then immediately releases it after a slight swipe ondown->onscroll->onscroll- >onscroll->.........->onfling Drag ondown->onscroll->onscroll->onfiling<br>E.onlongpress () Touch does not move always touch down when triggered<br>F.onfling () swipe gesture event; when you swipe a little distance, the parameter is triggered at Action_up: E1 1th Action_down motionevent and only one; E2 last Action_move motionevent; movement speed on Velocityx x-axis, pixels/sec, velocityy movement speed on y-axis, pixels/sec. trigger condition: The x-axis coordinate displacement is greater than fling_min_distance and moves faster than fling_min_ Velocity of pixels per second<br></p><p><p>2. Implement Interface: Ondoubletaplistener<br>A.onsingletapconfirmed () is used to determine if the click Is Singletap instead of doubletap, if the consecutive click is Doubletap gesture, if you only click once, The system waits for a period of time and does not receive a second click to determine that the click is Singletap instead of DOUBLETAP and then triggers the singletapconfirmed event. This method is different from onsingletapup, he is in Gesturedetector convinced that the user after the first touch of the screen, not immediately following the second touch screen, that is, not "double-click" when the Trigger.<br>Click on the very fast (do not Swipe) touchup:ondown->onsingletapup->onsingletapconfirmed<br>Click a little bit slower (don't Swipe) touchup:ondown->onshowpress->onsingletapup->onsingletapconfirmed<br>B.ondoubletap () Trigger When second touch down is clicked<br>C.ondoubletapevent () notifies events in the Doubletap gesture, including down, up, and move events (this refers to events that occur between double-clicks, such as double-clicking in the same place generates DOUBLETAP gestures, In the doubletap gesture there will also be a down and up event, both of which are notified by the function, and the second touch down and up of the double-click are triggered and can be distinguished by e.getaction ().<br></p></p><p><p>The 3.SimpleOnGestureListener class is a more convenient class for Gesturedetector to respond to different gestures, a class that implements both interfaces, and we can override arbitrary methods by inheriting Them.<br>Note: Each method has an instance of the Motionevent class as a parameter, which we can manipulate through this instance and class.<br><br>These are the basics, loading for beginners to learn, let their memories more profound, Welcome to read and Learn.<br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Basic Events</p></p></span>

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.