Mainactivity as follows:
Package Cn.c;import android.os.bundle;import Android.app.activity;import android.view.motionevent;/** * Demo Description: * Analyze the Android event distribution and processing mechanism * * In this example, three custom view is involved: * Outermost layout myframelayout * Inner layer layout mylinearlayout * Innermost custom button MyButton * in disp Atchtouchevent () Source code analysis refers to a very important thing: * If a view does not handle the Action_down event, that is, the event returned false (no consumption of the event) * So subsequent action_move and Action_ Up is no longer delivered to the view, meaning that the view is not eligible for processing action_move and ACTION_UP. * For this issue, verify here. * * * * MyButton return false directly in the Ontouchevent () method. * Then you can see that mybutton only handles Action_down. * Similar myframelayout and mylinearlayout also directly return false for touch events, and they can't handle it. * Action_move and ACTION_UP * */public class Mainactivity Extends Activity {@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancest ATE); Setcontentview (R.layout.main); System.out.println ("Call OnCreate ()" in ===> mainactivity); System.out.println ("--------------------------------------------------"); } @Override public boolean dispatchtouchevent (motionevent ev) {System.out.println ("Call Dispatchtouchevent ()" In "===> mainactivity"); System.out.println ("===> super.dispatchtouchevent () returns True by default"); System.out.println ("--------------------------------------------------"); return super.dispatchtouchevent (EV); } @Overridepublic void Onuserinteraction () {System.out.println ("Call Onuserinteraction ()" in ===> mainactivity); System.out.println ("--------------------------------------------------"); Super.onuserinteraction ();} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: System.out.println ("Call Ontouchevent ()--->action_down" in ===> mainactivity); Break;case Motionevent.action_move : System.out.println ("Call Ontouchevent ()--->action_move" in ===> mainactivity); Break;case motionevent.action_up: System.out.println ("Call Ontouchevent ()--->action_up" in ===> mainactivity);d Efault:break;} System.out.println ("Super.ontouchevent () returns false by default indicates no consumption event"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);}}
Myframelayout as follows:
Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.framelayout;public class Myframelayout extends Framelayout{public myframelayout (context context , AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {super.dispatchtouchevent (EV); System.out.println (called "dispatchtouchevent ()" in the outer myframelayout); System.out.println ("Super.dispatchtouchevent () returns true by default to indicate continued distribution"); System.out.println ("--------------------------------------------------"); return super.dispatchtouchevent (EV);// return false;} Overwrite from Viewgroup@overridepublic boolean onintercepttouchevent (motionevent ev) {System.out.println ("outer myframelayout Called Onintercepttouchevent () "); System.out.println ("super.onintercepttouchevent () default return false means no interception"); System.out.println ("--------------------------------------------------"); return super.onintercepttouchevent (EV) ;} Note://1 ViewGroup is a subclass of view//2 ViewGroup the Ontouchevent () method is returned by defaultFalse@overridepublic boolean ontouchevent (Motionevent event) {super.ontouchevent (event); switch (Event.getaction ()) { Case MotionEvent.ACTION_DOWN:System.out.println (called ontouchevent ()--->action_down "in the outer myframelayout); Case MotionEvent.ACTION_MOVE:System.out.println (called ontouchevent ()--->action_move "in the outer myframelayout); Case MotionEvent.ACTION_UP:System.out.println ("Call Ontouchevent ()--->action_up" in the outer myframelayout);d efault:break ;} System.out.println ("Super.ontouchevent () returns false by default indicates no consumption event"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);// return true;}}
Mylinearlayout as follows:
Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.linearlayout;public class Mylinearlayout extends LinearLayout {public mylinearlayout (Context Context, AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {super.dispatchtouchevent (EV); System.out.println ("Call Dispatchtouchevent ()" In the inner layer mylinearlayout); System.out.println ("Super.dispatchtouchevent () returns true by default to indicate continued distribution"); System.out.println ("--------------------------------------------------"); return super.dispatchtouchevent (EV);// return false;} Overwrite from Viewgroup@overridepublic boolean onintercepttouchevent (motionevent ev) {super.onintercepttouchevent (EV); System.out.println ("Call Onintercepttouchevent ()" In the inner layer mylinearlayout); System.out.println ("super.onintercepttouchevent () default return false means no interception"); System.out.println ("--------------------------------------------------"); return super.onintercepttouchevent (EV) ;} Note://1 ViewGroup is a viewThe subclass//2 ViewGroup in the Ontouchevent () method returns the False@overridepublic boolean ontouchevent (Motionevent event) {switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:System.out.println ("Call ontouchevent ()---in the inner layer mylinearlayout > Action_down "); Break;case MotionEvent.ACTION_MOVE:System.out.println (" Call Ontouchevent () in the inner Layer Mylinearlayout ()---> Action_move "); Break;case MotionEvent.ACTION_UP:System.out.println (" Call Ontouchevent () in the inner Layer Mylinearlayout ()---> Action_up ");d Efault:break;} System.out.println ("Super.ontouchevent () returns false by default indicates no consumption event"); System.out.println ("--------------------------------------------------"); return super.ontouchevent (event);}}
MyButton as follows:
Package Cn.c;import Android.content.context;import Android.util.attributeset;import android.view.MotionEvent; Import Android.widget.button;public class MyButton extends Button{public MyButton (context context, AttributeSet Attrs) { Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (Motionevent event) {System.out.println ("dispatchtouchevent" in the custom button ( )"); SYSTEM.OUT.PRINTLN ("Super.dispatchtouchevent returns True by default"); System.out.println ("--------------------------------------------------"); return Super.dispatchtouchevent (event) ;} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: System.out.println ("Call Ontouchevent ()--->action_down" in the custom button); Break;case motionevent.action_move: System.out.println ("Call Ontouchevent ()--->action_move" in the custom button); Break;case motionevent.action_up: System.out.println ("Call Ontouchevent ()--->action_up" in the custom button); break;default:break;} System.out.println ("--------------------------------------------------");//return false;return true;}}
Main.xml as follows:
<!--a custom layout, place a custom control--><cn.c.myframelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> < Cn.c.mylinearlayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <cn.c.mybutton android:layout_width= "200dip" android:layout_height= "200dip" android:text= " Custom Button " android:textcolor=" @android: Color/black " /> </cn.c.mylinearlayout></ Cn.c.myframelayout>
Android Event distribution in detail (vi)--action_down consumer verification