Android event processing

Source: Internet
Author: User

The event types in android include button events and screen Touch events. Touch events are the basic events of screen Touch events, so it is necessary to have a deep understanding of them.
A simple screen Touch action triggers a series of Touch events: ACTION_DOWN-> ACTION_MOVE...-> ACTION_MOVE-> ACTION_UP
When the screen contains a ViewGroup and the ViewGroup contains a sub-view, how does the android system handle Touch events? Is ViewGroup used to process Touch events or child views used to process Touch events? I can only say no to you for sure. Why? Let's take a look at my findings.
Each View subclass in the android system has the following three methods closely related to TouchEvent processing:
1) public boolean dispatchTouchEvent (MotionEvent ev) is used to distribute TouchEvent
2) public boolean onInterceptTouchEvent (MotionEvent ev) This method is used to intercept TouchEvent
3) The public boolean onTouchEvent (MotionEvent ev) method is used to process TouchEvent

When a TouchEvent occurs, the Activity first passes the TouchEvent to the top-level View. The TouchEvent first reaches the dispatchTouchEvent of the top-level view, and then is distributed by the dispatchTouchEvent method. If the dispatchTouchEvent returns true, the event is handled by onTouchEvent of the view. If dispatchTouchEvent returns false, the view's interceptTouchEvent method is used to determine whether to intercept the event. If interceptTouchEvent returns true, the event is intercepted, it will be handed over to its onTouchEvent for processing. If interceptTouchEvent returns false, it will be passed to the sub-view, and the dispatchTouchEvent of the sub-view will start this matter again. Distribution of parts. If the event is passed to the onTouchEvent of the subview of a certain layer, the method returns false, and the event will be passed up from this view, which is received by onTouchEvent. If false is returned when the onTouchEvent is passed to the top, the event will disappear and the next event will not be received.
The processing logic described by language is very abstract. I will describe it with code below.
Layout configuration file main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Test. lzqdiy. MyLinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center">
<Test. lzqdiy. MyTextView
Android: layout_width = "200px"
Android: layout_height = "200px"
Android: id = "@ + id/TV"
Android: text = "lzqdiy"
Android: textSize = "40sp"
Android: textStyle = "bold"
Android: background = "# FFFFFF"
Android: textColor = "# 0000FF"/>
</Test. lzqdiy. MyLinearLayout>
The node hierarchy is simple. A TextView is added to a LinearLayout.
The following is the java code:
Package test. lzqdiy;

Import android. app. Activity;
Import android. OS. Bundle;

Public class TestTouchEventApp extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
}
Package test. lzqdiy;

Import android. content. Context;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. widget. LinearLayout;

Public class MyLinearLayout extends LinearLayout {
Private final String TAG = "MyLinearLayout ";

Public MyLinearLayout (Context context, AttributeSet attrs ){

Super (context, attrs );

Log. d (TAG, TAG );

}

@ Override
Public boolean dispatchTouchEvent (MotionEvent ev ){
Int action = ev. getAction ();

Switch (action ){

Case MotionEvent. ACTION_DOWN:

Log. d (TAG, "dispatchTouchEvent action: ACTION_DOWN ");

Break;

Case MotionEvent. ACTION_MOVE:

Log. d (TAG, "dispatchTouchEvent action: ACTION_MOVE ");

Break;

Case MotionEvent. ACTION_UP:

Log. d (TAG, "dispatchTouchEvent action: ACTION_UP ");

Break;

Case MotionEvent. ACTION_CANCEL:

Log. d (TAG, "dispatchTouchEvent action: ACTION_CANCEL ");

Break;

}
Return super. dispatchTouchEvent (ev );
}

@ Override
Public boolean onInterceptTouchEvent (MotionEvent ev ){

Int action = ev. getAction ();

Switch (action ){

Case MotionEvent. ACTION_DOWN:

Log. d (TAG, "onInterceptTouchEvent action: ACTION_DOWN ");

Break;

Case MotionEvent. ACTION_MOVE:

Log. d (TAG, "onInterceptTouchEvent action: ACTION_MOVE ");

Break;

Case MotionEvent. ACTION_UP:

Log. d (TAG, "onInterceptTouchEvent action: ACTION_UP ");

Break;

Case MotionEvent. ACTION_CANCEL:

Log. d (TAG, "onInterceptTouchEvent action: ACTION_CANCEL ");

Break;

}

Return false;

}

@ Override
Public boolean onTouchEvent (MotionEvent ev ){

Int action = ev. getAction ();

Switch (action ){

Case MotionEvent. ACTION_DOWN:

Log. d (TAG, "--- onTouchEvent action: ACTION_DOWN ");

Break;

Case MotionEvent. ACTION_MOVE:

Log. d (TAG, "--- onTouchEvent action: ACTION_MOVE ");

Break;

Case MotionEvent. ACTION_UP:

Log. d (TAG, "--- onTouchEvent action: ACTION_UP ");

Break;

Case MotionEvent. ACTION_CANCEL:

Log. d (TAG, "--- onTouchEvent action: ACTION_CANCEL ");

Break;

}

Return true;
}

}

Package test. lzqdiy;

Import android. content. Context;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. widget. TextView;

Public class MyTextView extends TextView {

Private final String TAG = "MyTextView ";

Public MyTextView (Context context, AttributeSet attrs ){

Super (context, attrs );

}

@ Override
Public boolean dispatchTouchEvent (MotionEvent ev ){
Int action = ev. getAction ();

Switch (action ){

Case MotionEvent. ACTION_DOWN:

Log. d (TAG, "dispatchTouchEvent action: ACTION_DOWN ");

Break;

Case MotionEvent. ACTION_MOVE:

Log. d (TAG, "dispatchTouchEvent action: ACTION_MOVE ");

Break;

Case MotionEvent. ACTION_UP:

Log. d (TAG, "dispatchTouchEvent action: ACTION_UP ");

Break;

Case MotionEvent. ACTION_CANCEL:

Log. d (TAG, "onTouchEvent action: ACTION_CANCEL ");

Break;

}
Return super. dispatchTouchEvent (ev );
}

@ Override
Public boolean onTouchEvent (MotionEvent ev ){

Int action = ev. getAction ();

Switch (action ){

Case MotionEvent. ACTION_DOWN:

Log. d (TAG, "--- onTouchEvent action: ACTION_DOWN ");

Break;

Case MotionEvent. ACTION_MOVE:

Log. d (TAG, "--- onTouchEvent action: ACTION_MOVE ");

Break;

Case MotionEvent. ACTION_UP:

Log. d (TAG, "--- onTouchEvent action: ACTION_UP ");

Break;

Case MotionEvent. ACTION_CANCEL:

Log. d (TAG, "--- onTouchEvent action: ACTION_CANCEL ");

Break;

}

Return true;

}

}

For reference convenience, the following describes MyLinearLayout as L and MyTextView as T, L. onInterceptTouchEvent = true indicates that the onInterceptTouchEvent method in MyLinearLayout returns true. The call sequence is described by the Log output when the program is running.
In 1st cases, L. onInterceptTouchEvent = false & L. onTouchEvent = true & T. onTouchEvent = true outputs the following Log:
D/MyLinearLayout (11865): dispatchTouchEvent action: ACTION_DOWN
D/MyLinearLayout (11865): onInterceptTouchEvent action: ACTION_DOWN
D/MyTextView (11865): dispatchTouchEvent action: ACTION_DOWN
D/MyTextView (11865): --- onTouchEvent action: ACTION_DOWN
D/mylinelayout (11865): dispatchTouchEvent action: ACTION_MOVE
D/mylinelayout (11865): onInterceptTouchEvent action: ACTION_MOVE
D/MyTextView (11865): dispatchTouchEvent action: ACTION_MOVE
D/MyTextView (11865): --- onTouchEvent action: ACTION_MOVE
...... Omitting other ACTION_MOVE Event Logs
D/MyLinearLayout (11865): dispatchTouchEvent action: ACTION_UP
D/MyLinearLayout (11865): onInterceptTouchEvent action: ACTION_UP
D/MyTextView (11865): dispatchTouchEvent action: ACTION_UP
D/MyTextView (11865): --- onTouchEvent action: ACTION_UP
Conclusion: TouchEvent is completely processed by TextView.
In 2nd cases, L. onInterceptTouchEvent = false & L. onTouchEvent = true & T. onTouchEvent = false outputs the following Log:
D/MyLinearLayout (13101): dispatchTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13101): onInterceptTouchEvent action: ACTION_DOWN
D/MyTextView (13101): dispatchTouchEvent action: ACTION_DOWN
D/MyTextView (13101): --- onTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13101): --- onTouchEvent action: ACTION_DOWN
D/mylinelayout (13101): dispatchTouchEvent action: ACTION_MOVE
D/MyLinearLayout (13101): --- onTouchEvent action: ACTION_MOVE
...... Omitting other ACTION_MOVE Event Logs
D/MyLinearLayout (13101): dispatchTouchEvent action: ACTION_UP
D/MyLinearLayout (13101): --- onTouchEvent action: ACTION_UP
Conclusion: TextView only processes ACTION_DOWN events and LinearLayout processes all touchevents.
In 3rd cases, L. onInterceptTouchEvent = true & L. onTouchEvent = true outputs the following Log:
D/MyLinearLayout (13334): dispatchTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13334): onInterceptTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13334): --- onTouchEvent action: ACTION_DOWN
D/mylinelayout (13334): dispatchTouchEvent action: ACTION_MOVE
D/MyLinearLayout (13334): --- onTouchEvent action: ACTION_MOVE
...... Omitting other ACTION_MOVE Event Logs
D/MyLinearLayout (13334): dispatchTouchEvent action: ACTION_UP
D/MyLinearLayout (13334): --- onTouchEvent action: ACTION_UP
Conclusion: LinearLayout processes all touchevents.
In 4th cases, L. onInterceptTouchEvent = true & L. onTouchEvent = false outputs the following Log:
D/MyLinearLayout (13452): dispatchTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13452): onInterceptTouchEvent action: ACTION_DOWN
D/MyLinearLayout (13452): --- onTouchEvent action: ACTION_DOWN
Conclusion: LinearLayout only processes the ACTION_DOWN event. Who will handle other touchevents? The answer is that the Activity at the outermost layer of LinearLayout processes TouchEvent.

Author "xfjt297857539"
 

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.