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
- Package com. tutor. touch;
- Import android. content. Context;
- Import android. util. AttributeSet;
- Import android. util. Log;
- Import android. view. MotionEvent;
- Import android. widget. FrameLayout;
- Public class MyLayout extends FrameLayout {
- Public MyLayout (Context context ){
- Super (context );
- }
- Public MyLayout (Context context, AttributeSet attrs ){
- Super (context, attrs );
- // TODO Auto-generated constructor stub
- }
- @ Override
- Public boolean onInterceptTouchEvent (MotionEvent ev ){
- Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent .");
- Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent default return"
- + Super. onInterceptTouchEvent (ev ));
- Return super. onInterceptTouchEvent (ev );
- }
- @ Override
- Public boolean onTouchEvent (MotionEvent event ){
- Log. e (TouchDemoActivity. TAG, "MyLayout onTouchEvent .");
- Log. e (TouchDemoActivity. TAG, "MyLayout onTouchEvent default return"
- + Super. onTouchEvent (event ));
- Return super. onTouchEvent (event );
- }
- }
Create a new MyView. java code as follows:
[Java]View plaincopy
- Package com. tutor. touch;
- Import android. content. Context;
- Import android. util. AttributeSet;
- Import android. util. Log;
- Import android. view. MotionEvent;
- Import android. widget. Button;
- Public class MyView extends Button {
- Public MyView (Context context ){
- Super (context );
- }
- Public MyView (Context context, AttributeSet attrs ){
- Super (context, attrs );
- }
- @ Override
- Public boolean onTouchEvent (MotionEvent event ){
- Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent .");
- Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent default return"
- + Super. onTouchEvent (event ));
- Return super. onTouchEvent (event );
- }
- }
The TouchDemoActivity code is as follows:
[Java]View plaincopy
- Package com. tutor. touch;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Public class TouchDemoActivity extends Activity {
- Public static final String TAG = "TouchDemoActivity ";
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- }
- }
The main. xml Code of all the above layout files is as follows:
[Java]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Com. tutor. touch. MyLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Com. tutor. touch. MyView
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/hello"/>
- </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
- @ Override
- Public boolean onInterceptTouchEvent (MotionEvent ev ){
- Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent .");
- Log. e (TouchDemoActivity. TAG, "MyLayout onInterceptTouchEvent default return"
- + Super. onInterceptTouchEvent (ev ));
- Return true;
- }
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
- @ Override
- Public boolean onTouchEvent (MotionEvent event ){
- Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent .");
- Log. e (TouchDemoActivity. TAG, "MyView onTouchEvent default return"
- + Super. onTouchEvent (event ));
- Return false;
- }
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.