Android event distribution (4)-basic event transfer example
MainActivity is 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 transmission basics ** Background: * The Event transmission direction is: from the outermost layer (Activity) to the innermost layer (a View) * The consumption direction of the event is: pass from the innermost layer (a View) to the outermost layer (Activity). *** Demo composition: * 1 a common Activity contains a Custom Button. * 2 override dispatchTouchEvent of an Activity and onTouchEvent * 3 override dispatchTouchEvent and onTouchEvent * both return values (true ). * 4 register TouchListener for the Custom Button in the Activity, * overwrite onTouch () in it. By default, this method returns false, indicating that the event is not consumed. * 5 register ClickListener for the Custom Button in the Activity, and ** click the Button on the screen to test the processing sequence of the Touch event: * 1 dispatchTouchEvent () of the Activity () method * if it returns false to indicate that the event is not distributed, the dispatchTouchEvent () method of the * 2 Custom Button is not executed in 2,3 and 4 * if it returns false, it indicates that the event is not distributed, 3, 4, and 4 will not execute the TouchListener of the Custom Button * 3 Activity * if it returns true, it indicates that the event has been consumed, 4 will not execute the onTouchEvent () of the * 4 Custom Button () method * if it returns false (the event is not consumed), a false value is returned to the dispatchTouchEvent () method of the Custom Button. * Custom buttons interrupt event distribution, that is, only ACTION_DOWN events are distributed each time, But ACTION_MOVE and ACTION_UP are not distributed. * therefore, each Custom Button only responds to ACTION_DOWN and does not receive ACTION_MOVE and ACTION_UP. * Because these events are not consumed, the event will be passed up, so the onTouchEvent () method of the Activity * can respond to a series of ACTION_DOWN, ACTION_MOVE, and ACTION_UP. * if it returns true (the event is consumed), the event will not be passed to the Activity. * Activity naturally does not receive a series of ACTION_DOWN, ACTION_MOVE, and ACTION_UP. ** conclusion: * (1) Every ACTION_DOWN, ACTION_MOVE, and ACTION_UP will cause dispatchTouchEvent () * (2) at each layer, but onTouchEvent () at each layer () it will not be executed. for example, if the lower layer has consumed the event, the upper layer will not respond to the onTouchEvent. ** 5. The onClick () method in the ClickListener of the Custom Button *** Summary: * 1 because the onTouchEvent of the Custom Button is returned as true by default. * indicates that the event has been consumed and will not be passed to the upper layer. Therefore, the onTouchEvent method in the Activity will not call * 2. Prove again: * onTouch () in View prior to onTouchEvent () in the execution * View, onTouchEvent () is executed before onClick (), or onClick () * 3 1 and 2 are called in onTouchEvent (), which indicates: event Transmission Direction --> transfer from the outermost layer (Activity) to the innermost layer (a View) ****** constant: * ACTION_DOWN -----> 0 * ACTION_UP -------> 1 * ACTION_MOVE -----> 2 ** reference: www.bkjia.com * Thank you very much **/public class MainActivity extends Activity {private EventButton 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 (---> the OnTouchListener + event of the button. getAction (); return false ;}}); mEventButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) {System. out. println (---> the OnClickListener of the Button clicks Button) ;}}) ;}@ 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 onTouchEvent + event. getAction () +, it defaulst is false); return super. onTouchEvent (event );}}
EventButton is 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 (---> button dispatchTouchEvent, it defaulst is true); return super. dispatchTouchEvent (event);} @ Overridepublic boolean onTouchEvent (MotionEvent event) {System. out. println (---> button onTouchEvent, it defaulst is true); return super. onTouchEvent (event); // return false ;}}
Http://www.bkjia.com/kf/201412/365603.html previous