OnInterceptTouchEvent, onTouchEvent, and ontouchevent in Android

Source: Internet
Author: User

OnInterceptTouchEvent, onTouchEvent, and ontouchevent in Android

OnInterceptTouchEvent:

OnInterceptTouchEvent is defined in ViewGroup. The layout class in Android generally inherits this class. OnInterceptTouchEvent is used to intercept gesture events. Each gesture event first calls onInterceptTouchEvent.

OnTouchEvent:

OnTouchEvent is also a method defined in view. Process the gesture events passed to the view. The types of gesture events include ACTION_DOWN, ACTION_MOVE, ACTION_UP, and ACTION_CANCEL.

The default value of onInterceptTouchEvent in Layout is false, so that the touch event is passed to the View control. The default value of onTouch in Layout is false, and the default value of onTouch in View is true, when we click the screen with our fingers, we call the ACTION_DOWN event first. When the return value in onTouch is true, onTouch continues to call the ACTION_UP event. If the return value in onTouch is false, onTouch only calls ACTION_DOWN instead of ACTION_UP.

In order to make it easier for the House to understand, I wrote a simple Demo and customized Layout and View. The Android project directory is as follows:

The code for creating a new MyLayout. java is as follows:


[Java]View plaincopy
  1. Package com. tutor. touch;
  2. Import android. content. Context;
  3. Import android. util. AttributeSet;
  4. Import android. util. Log;
  5. Import android. view. MotionEvent;
  6. Import android. widget. FrameLayout;
  7. Public class MyLayout extends FrameLayout {
  8. Public MyLayout (Context context ){
  9. Super (context );
  10. }
  11. Public MyLayout (Context context, AttributeSet attrs ){
  12. Super (context, attrs );
  13. // TODO Auto-generated constructor stub
  14. }
  15. @ Override
  16. Public boolean onInterceptTouchEvent (MotionEvent ev ){
  17. Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent .");
  18. Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent default return"
  19. + Super. onInterceptTouchEvent (ev ));
  20. Return super. onInterceptTouchEvent (ev );
  21. }
  22. @ Override
  23. Public boolean onTouchEvent (MotionEvent event ){
  24. Log. e (TouchDemoActivity. TAG, "MyLayout onTouchEvent .");
  25. Log. e (TouchDemoActivity. TAG, "MyLayout onTouchEvent default return"
  26. + Super. onTouchEvent (event ));
  27. Return super. onTouchEvent (event );
  28. }
  29. }

Create a new MyView. java code as follows:

[Java]View plaincopy
  1. Package com. tutor. touch;
  2. Import android. content. Context;
  3. Import android. util. AttributeSet;
  4. Import android. util. Log;
  5. Import android. view. MotionEvent;
  6. Import android. widget. Button;
  7. Public class MyView extends Button {
  8. Public MyView (Context context ){
  9. Super (context );
  10. }
  11. Public MyView (Context context, AttributeSet attrs ){
  12. Super (context, attrs );
  13. }
  14. @ Override
  15. Public boolean onTouchEvent (MotionEvent event ){
  16. Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent .");
  17. Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent default return"
  18. + Super. onTouchEvent (event ));
  19. Return super. onTouchEvent (event );
  20. }
  21. }

The TouchDemoActivity code is as follows:

[Java]View plaincopy
  1. Package com. tutor. touch;
  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Public class TouchDemoActivity extends Activity {
  5. Public static final String TAG = "TouchDemoActivity ";
  6. @ Override
  7. Public void onCreate (Bundle savedInstanceState ){
  8. Super. onCreate (savedInstanceState );
  9. SetContentView (R. layout. main );
  10. }
  11. }

The main. xml Code of all the above layout files is as follows:

[Java]View plaincopy
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Com. tutor. touch. MyLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. >
  6. <Com. tutor. touch. MyView
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "@ string/hello"/>
  10. </Com. tutor. touch. MyLayout>

The following figure shows the effects of running the Android project:

Click the red area to trigger the onTouch event in MyView to view logcat, for example:

Click the green area to trigger the onTouch event in MyLayout and view logcat, as shown in:

Both of the above use the system default value. It can be concluded that the default value of onInterceptTouchEvent is false, and the default value of onTouchEvent in MyLayout is false. Therefore, only ACTION_DOWN events are consumed, in MyView, the default value returned by onTouch is true. It is called twice: ACTION_DOW and ACTION_UP.

Next we will change the return value of onInterceptTouchEvent in MyLayout. java to true. The Code is as follows:

[Java]View plaincopy
  1. @ Override
  2. Public boolean onInterceptTouchEvent (MotionEvent ev ){
  3. Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent .");
  4. Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent default return"
  5. + Super. onInterceptTouchEvent (ev ));
  6. Return true;
  7. }

Run the project, continue to click the red area, view logcat, and find that the onTouch event of MyView is not called, that is, it is intercepted, as shown in:

Let's continue the experiment and set the returned value of onInterceptTouchEvent to false. Change the returned value of onTouchEvent in MyView to false, that is, the onTouchEvent in MyView is modified as follows:

[Java]View plaincopy
  1. @ Override
  2. Public boolean onTouchEvent (MotionEvent event ){
  3. Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent .");
  4. Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent default return"
  5. + Super. onTouchEvent (event ));
  6. Return false;
  7. }

Run the project and click the red area to view logcat, for example:

We can see that the OnTouchEvent in MyView only consumes one click event (ACTION_DOWN), does not execute ACTION_UP, and then runs to MyLayout to execute the OnTouchEvent event.

Therefore, the summary is as follows:

The default value of onInterceptTouchEvent in ViewGroup is false to pass the event to onTouchEvent in View.

The default onTouchEvent value in ViewGroup is false.

The default value of onTouchEvent returned in the View is true. In this way, multiple touch events can be executed.


What is the use of onTouchEvent and onTouch in android programming ??

I. onTouch
OnTouch is the method in the OnTouchListener interface of the View. It processes the View and its subclass by the touch event. Of course, the premise is that the touch time can be passed to the specified view. Q1: Why cannot it be passed?
1 :/**
2: * Interface definition for a callback to be invoked when a touch event is
3: * dispatched to this view. The callback will be invoked before the touch
4: * event is given to the view.
5 :*/
6: public interface OnTouchListener {
7 :/**
8: * Called when a touch event is dispatched to a view. This allows listeners
9: * get a chance to respond before the target view.
10 :*
11: * @ param v The view the touch event has been dispatched.
12: * @ param event The MotionEvent object containing full information about
13: * the event.
14: * @ return True if the listener has consumed the event, false otherwise.
15 :*/
16: boolean onTouch (View v, MotionEvent event );
17 :}
Ii. onTouchEvent
OnTouchEvent is also a method defined in view. Process the gesture events passed to the view. The types of gesture events include ACTION_DOWN, ACTION_MOVE, ACTION_UP, and ACTION_CANCEL.
1 :/**
2: * Implement this method to handle touch screen motion events.
3 :*
4: * @ param event The motion event.
5: * @ return True if the event was handled, false otherwise.
6 :*/
7: public boolean onTouchEvent (MotionEvent event ){
8 :......
9 :......
10 :}
Once the onTouchEvent method is called and true is returned, the gesture event ends and is not passed down to the Child control. Q2: When will onTouchEvent be called?
3. onInterceptTouchEvent
OnInterceptTou ...... remaining full text>
 
Android inherited activity cannot override onInterceptTouchEvent?

The Activity is not a parent class view. There is no onInterceptTouchEvent method.
In Android, only views that can be used as parents can have onInterceptTouchEvent.
This means that sub-events are blocked from being passed to the sub-view. If you want to intercept Touch events, you can rewrite them.
DispatchTouchEvent of the Activity, which is the first entry of Touch on the surface.

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.