Android Touch event Transfer Mechanism

Source: Internet
Author: User

Mainactivity is as follows:

Package cn. c; import android. OS. bundle; import android. app. activity; import android. view. menu; // The first test // The output result is as follows: // call onInterceptTouchEvent () in MyFrameLayout // onInterceptTouchEvent () in MyFrameLayout returns by default: false // call onTouchEvent () in the Custom Button // The onTouchEvent () returned by default in the Custom Button is: true // onTouchEvent () ---> ACTION_DOWN // call onInterceptTouchEvent () in MyFrameLayout // onInterceptTouchEvent () in MyFrameLayout () The default value returned is false // call onTouchEvent () in the Custom Button // onTouchEvent () in the Custom Button. The default value returned is true // onTouchEvent () in the Custom Button () ---> ACTION_UP // Summary: // 1 This is the output after you click the Custom Button quickly. We can see: // onTouchEvent () in the Button () the onInterceptTouchEvent () // 2 will be called before ACTION_DOWN // and ACTION_UP. If the time is long, the following code will be repeatedly executed between ACTION_DOWN and ACTION_UP: // call onInterceptTouchEvent () in MyFrameLayout // onInterceptTouchEvent () in MyFrameLayout returns false by default. // call o in Custom Button NTouchEvent () // The onTouchEvent () returned by default in the Custom Button is true // 3 from this we can see: // onInterceptTouchEvent () the default returned is false // No interception. // The onTouchEvent () of the Custom Button is returned as true by default. // you can respond to the click event. // 4. Sorting: The Custom Button responds to onTouchEvent () because no interception is detected () // so in MyFrameLayout, The onTouchEvent () is not returned // The second test // onInterceptTouchEvent (MotionEvent ev) returns true // that is, blocking // at this time, the Custom Button does not respond to onTouchEvent () // but at this time, MyFrameLayout will respond to onTouchEvent () // but only responds to ACTION_DOWN and does not respond to ACTIO N_UP // because onTouchEvent () in MyFrameLayout returns false by default. // to make MyFrameLayout respond to onTouchEvent () and execute // ACTION_DOWN and ACTION_UP. in MyFrameLayout, The onTouchEvent () in // returns true // The third test // returns false to onInterceptTouchEvent () in MyFrameLayout (default) // make the onTouchEvent () in the Custom Button return false // The execution result is: onTouchEvent () in the Custom Button only executes ACTION_DOWN // The onTouchEvent () in the Button is not executed () ACTION_UP. then, // executes onTouchEvent () in MyFrameLayout and only responds to ACTION_DO. WN. // Why: onTouchEvent () in MyFrameLayout only responds to ACTION_DOWN ??? // As mentioned above: onTouchEvent () in MyFrameLayout returns false by default. // If the returned value is changed to true, // ACTION_DOWN and ACTION_UP in onTouchEvent () in MyFrameLayout will be executed. // Test Summary: // 1 onInterceptTouchEvent () in MyFrameLayout returns false by default // that is, it does not intercept, so that the click event is easily passed to onTouchEvent () in View () // 2 The onTouchEvent () in MyFrameLayout returns false by default. // 3 The onTouchEvent () in the Custom View returns true by default, so that OnTouch () can be returned multiple times () event // reference: // http://blog.csdn.net/psh24053/article/details/7523486// http://blog.csdn.net/android_tutor/article/details/7193090public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Myframelayout is as follows:

Package cn. c; import android. content. context; import android. util. attributeSet; import android. view. keyEvent; import android. view. motionEvent; import android. widget. frameLayout; public class MyFrameLayout extends FrameLayout {public MyFrameLayout (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overridepublic boolean dispatchKeyEvent (KeyEvent event) {// TODO Auto-generated method stubreturn super. dispatchKeyEvent (event);} // override from ViewGroup @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {System. out. println ("calling onInterceptTouchEvent ()" In MyFrameLayout); System. out. println ("onInterceptTouchEvent () in MyFrameLayout returns:" + super. onInterceptTouchEvent (ev); // The following sentence corresponds to the first test return super. onInterceptTouchEvent (ev); // The following sentence corresponds to the second test // return true; // The following sentence corresponds to the third test // return super. onInterceptTouchEvent (ev);} // override from View // However, ViewGroup is a subclass of View @ Overridepublic boolean onTouchEvent (MotionEvent event) {// For the second test System. out. println ("calling onTouchEvent ()" In MyFrameLayout); System. out. println ("onTouchEvent () in MyFrameLayout returns:" + super. onInterceptTouchEvent (event); switch (event. getAction () {case MotionEvent. ACTION_DOWN: System. out. println ("onTouchEvent () ---> ACTION_DOWN" in MyFrameLayout); break; case MotionEvent. ACTION_UP: System. out. println ("onTouchEvent () ---> ACTION_UP" in MyFrameLayout); default: break;} 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, attributeset attrs) {super (context, attrs ); // todo auto-generated constructor stub} @ overridepublic Boolean ontouchevent (motionevent event) {system. out. println ("calling ontouchevent ()" In a Custom button); system. out. println ("ontouchevent () in a Custom button returns:" + super. ontouchevent (event); Switch (event. getaction () {Case motionevent. action_down: system. out. println ("ontouchevent () ---> action_down"); break; Case motionevent. action_up: system. out. println ("ontouchevent () ---> action_up" in the Custom button); default: break;} // The following sentence corresponds to the first, second test return Super. ontouchevent (event); // The following sentence corresponds to the third test // return false ;}}

Main. XML is as follows:

<! -- Place a custom control in the custom Layout --> <CN. c. myframelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <CN. c. mybutton Android: layout_width = "200dip" Android: layout_height = "200dip" Android: text = "Custom button" Android: textcolor = "@ Android: color/Black "/> </CN. c. myframelayout>

 

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.