Android Event distribution in detail (iv)--Example of event delivery basics

Source: Internet
Author: User

Mainactivity as follows:
Package Com.cn;import Android.app.activity;import Android.os.bundle;import android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import android.view.view.ontouchlistener;/** * Demo Description: * Android Event Delivery Basics * Background: * The event is passed in the direction of consumption from the outermost (activity) to the most inner (a view) * Event: From the topmost (a view) to the outermost layer (activity) * * * * Demo composition : * 11 Common activity contains a custom button. * 2 Overwrite activity dispatchtouchevent and Ontouchevent * 3 custom button overwrite Dispatchtouchevent and ontouchevent * Both return values are default values (TRUE). * 4 in activity for the custom button registration Touchlistener, * Overwrite the inside of the Ontouch (), the method by default returns False to indicate that the event is not consumed. * 5 in Activity for custom button registration Clicklistener, * * * Click on the screen button to test * * * Touch Event Processing Order: * 1 activity of dispatchtouchevent () Method * If it returns false to indicate that the event is not distributed, then 2,3,4 will not execute the * 2 custom button's Dispatchtouchevent () method * If it returns false to indicate that the event is not distributed, then 3, 4 will not execute * 3 activity in custom BU Tton Touchlistener Execution * If it returns true to indicate that the event has been consumed, then 4 will not execute the * 4 custom button's Ontouchevent () method * If it returns False (the event is not consumed) then the disp returned to the custom button The Atchtouchevent () method is a false. * So a custom button interrupts the distribution of the event, that is, each time it is dividedAction_down events, do not distribute Action_move and action_up. * Therefore, each custom button will only respond to Action_down and will not receive Action_move and ACTION_UP. * Also because these events are not consumed, the event is passed up, so the activity's Ontouchevent () method * can respond to a series of Action_down and Action_move as well as action_up. * If it returns True (the event is consumed), then the event will not be passed up to the activity. * Activity naturally also received a series of Action_down and Action_move as well as action_up. * Summary: * (1) Each time the Action_down and Action_move and action_up will cause each layer of dispatchtouchevent () * (2) But each layer of ontouchevent () is not necessarily Ok. For example, the lower layer has already consumed the event, then the upper layer will not respond to ontouchevent (). * * 5 The OnClick () method in the Clicklistener of the custom button * * Summary: * 1 because the Ontouc of the custom button Hevent returns to True by default. * Indicates that the event has been consumed and will not continue to pass upward, so the Ontouchevent method in activity does not call * 2 to prove again: * View Ontouch () before ontouchevent () execution * View Ontouchevent () execute before onclick (), or call OnClick () in Ontouchevent () * 3 1 and 2 for the event's delivery direction--from the outermost layer (Activity) to the most inner (a view) * * * * constant corresponding : * Action_down----->0 * action_up------->1 * Action_move----->2 * * Reference: * 1 http://blog.csdn.net/bigconvience /article/details/26391743 * 2 Http://blog.csdn.net/siobhan/article/details/8257334 * 3 http://blog.csdn.net/drunkcello/article/details/39892761 * 4 http://blog.csdn.net/yuanzeyao/ article/details/37961997 * 5 http://blog.csdn.net/lmj623565791/article/details/38960443 * 6 http://blog.csdn.net/ lmj623565791/article/details/39102591 * Thank you very much * */public class Mainactivity extends Activity {private Eve Ntbutton Meventbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (   Savedinstancestate); Setcontentview (R.layout.main); init ();}   private void Init () {meventbutton= (Eventbutton) Findviewbyid (R.id.button); Meventbutton.setontouchlistener (New Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {   SYSTEM.OUT.PRINTLN ("---> Button ontouchlistener" +event.getaction ()); return false;}}); Meventbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (view view) {System.out.println ("   ---> button "Onclicklistener clicked");}      }); } @Override Public Boolean dispatchtouchevent (motionevent ev) {SYSTEM.OUT.PRINTLN ("---> Activity dispatchtouchevent" +ev.getaction () + ", it    Defaulst is true ");    return super.dispatchtouchevent (EV); } @Override public boolean ontouchevent (Motionevent event) {System.out.println ("---> Activity ontoucheven    T "+event.getaction () +", it Defaulst is false ");    Return Super.ontouchevent (event); }    }

Eventbutton as follows:
Package Com.cn;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.button;public class Eventbutton extends Button {public Eventbutton (context context, AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (Motionevent event) {System.out.println ("---> Buttons dispatchtouchevent, it Defaulst is true "); return super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (Motionevent event) {System.out.println ("---> Buttons ontouchevent  , it defaulst is true "); return super.ontouchevent (event);//return false;}}

Main.xml as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <com.cn.eventbutton        android:id=" @+id/button "        android:layout_width=" 250dip "        android:layout_height= "250dip"        android:layout_centerinparent= "true"        android:text= "Touch Me"        / ></RelativeLayout>


Android Event distribution in detail (iv)--Example of event delivery basics

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.