Android event distribution (6) -- ACTION_DOWN consumption Verification

Source: Internet
Author: User

Android event distribution (6) -- ACTION_DOWN consumption Verification
MainActivity is as follows:

Package cn. c; import android. OS. bundle; import android. app. activity; import android. view. motionEvent;/*** Demo Description: * analyzes the Android event distribution and processing mechanism **. In this example, three custom views are involved. they are: * The layout of the outermost layer MyFrameLayout * the layout of the inner layer MyLinearLayout * The Custom button MyButton of the innermost layer ** mentioned a very important thing in the dispatchTouchEvent () source code analysis: * If a View does not process the ACTION_DOWN event, that is, false is returned for the event (the event is not consumed) * Subsequent ACTION_MOVE and ACTION_UP operations will not be passed to the View; that is to say, this View does not have * the qualification to process ACTION_MOVE and ACTION_UP. * This issue is verified here. * ** in the onTouchEvent () method of MyButton, false is directly returned. * Then we can see that MyButton only processes ACTION_DOWN. * Similarly, MyFrameLayout and MyLinearLayout directly return false for Touch events; they cannot process * ACTION_MOVE and ACTION_UP **/public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); System. out. println (==> MainActivity calls onCreate (); System. out. println (------------------------------------------------);} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {System. out. println (==> MainActivity calls dispatchTouchEvent (); System. out. println (==> super. dispatchTouchEvent () returns true by default); System. out. println (--------------------------------------------------); return super. dispatchTouchEvent (ev) ;}@ Overridepublic void onUserInteraction () {System. out. println (==> MainActivity calls onUserInteraction (); System. out. println (--------------------------------------------------); super. onUserInteraction () ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: System. out. println (==> MainActivity calls onTouchEvent () ---> ACTION_DOWN); break; case MotionEvent. ACTION_MOVE: System. out. println (==> MainActivity calls onTouchEvent () ---> ACTION_MOVE); break; case MotionEvent. ACTION_UP: System. out. println (==> MainActivity calls onTouchEvent () ---> ACTION_UP); default: break;} System. out. println (super. onTouchEvent () returns false by default, indicating that the event is not consumed); System. out. println (--------------------------------------------------); return super. onTouchEvent (event );}}

MyFrameLayout is as follows:
Package cn. c; import android. content. context; import android. util. attributeSet; import android. view. motionEvent; import android. widget. frameLayout; public class extends FrameLayout {public MyFrameLayout (Context context, AttributeSet attrs) {super (context, attrs);} @ Overridepublic boolean dispatchTouchEvent (MotionEvent ev) {super. dispatchTouchEvent (ev); System. out. println (the dispatchTouchEvent () is called in the outer MyFrameLayout; System. out. println (super. dispatchTouchEvent () returns true by default to continue distribution); System. out. println (--------------------------------------------------); return super. dispatchTouchEvent (ev); // return false;} // overwrite from ViewGroup @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {System. out. println (calling onInterceptTouchEvent () in the outer MyFrameLayout); System. out. println (super. onInterceptTouchEvent () returns false by default to prevent interception); System. out. println (--------------------------------------------------); return super. onInterceptTouchEvent (ev);} // Note: // 1 ViewGroup is a subclass of View // The onTouchEvent () method in 2 ViewGroup returns false @ Overridepublic boolean onTouchEvent (MotionEvent event) by default) {super. onTouchEvent (event); switch (event. getAction () {case MotionEvent. ACTION_DOWN: System. out. println (calling onTouchEvent () ---> ACTION_DOWN) in the outer MyFrameLayout; break; case MotionEvent. ACTION_MOVE: System. out. println (the outer MyFrameLayout calls onTouchEvent () ---> ACTION_MOVE); break; case MotionEvent. ACTION_UP: System. out. println (calling onTouchEvent () ---> ACTION_UP in the outer MyFrameLayout); default: break;} System. out. println (super. onTouchEvent () returns false by default, indicating that the event is not consumed); System. out. println (--------------------------------------------------); return super. onTouchEvent (event); // return true ;}}

 

MyLinearLayout is 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. dispatchTouchEvent (ev); System. out. println (calls dispatchTouchEvent () in the inner MyLinearLayout); System. out. println (super. dispatchTouchEvent () returns true by default to continue distribution); System. out. println (--------------------------------------------------); return super. dispatchTouchEvent (ev); // return false;} // overwrite from ViewGroup @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {super. onInterceptTouchEvent (ev); System. out. println (calls onInterceptTouchEvent () in the inner MyLinearLayout); System. out. println (super. onInterceptTouchEvent () returns false by default to prevent interception); System. out. println (--------------------------------------------------); return super. onInterceptTouchEvent (ev);} // Note: // 1 ViewGroup is a subclass of View // The onTouchEvent () method in 2 ViewGroup returns false @ Overridepublic boolean onTouchEvent (MotionEvent event) by default) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: System. out. println (calls onTouchEvent () ---> ACTION_DOWN in the inner MyLinearLayout); break; case MotionEvent. ACTION_MOVE: System. out. println (calls onTouchEvent () ---> ACTION_MOVE in the inner MyLinearLayout); break; case MotionEvent. ACTION_UP: System. out. println (calls onTouchEvent () ---> ACTION_UP in the inner MyLinearLayout); default: break;} System. out. println (super. onTouchEvent () returns false by default, indicating that the event is not consumed); System. out. println (--------------------------------------------------); return super. onTouchEvent (event );}}


 

MyButton is 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 (call 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); break; case MotionEvent in the Custom Button. ACTION_MOVE: System. out. println (call onTouchEvent () ---> ACTION_MOVE); break; case MotionEvent in the Custom Button. ACTION_UP: System. out. println (call onTouchEvent () ---> ACTION_UP); break; default: break;} System. out. println (--------------------------------------------------); // return false; return true ;}}

     
     
   
    
Http://www.bkjia.com/kf/201412/365609.html previous
   
  
 


 

Related Article

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.