Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle Analysis of Click event Distribution Mechanism in Android

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Android click event distribution mechanism principle analysis related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone reading this Android click event distribution mechanism principle analysis article will have some gains, let's take a look at it.

Building the simplest structure

Create an Activity, rewrite dispatchTouchEvent and onTouchEvent, the former method is responsible for the distribution of click events, the latter method is responsible for the consumption of click events, and then print the triggers of three touch events

private static final String TAG = MainActivity.class.getSimpleName();@Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "Activity onTouchEvent ACTION_DOWN");//press break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "Activity onTouchEvent ACTION_MOVE");//Move break; case MotionEvent.ACTION_UP: Log.d(TAG, "Activity onTouchEvent ACTION_UP");//Lift break; } return super.onTouchEvent(event); }@Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "Activity dispatchTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "Activity dispatchTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, "Activity dispatchTouchEvent ACTION_UP"); break; } return super.dispatchTouchEvent(ev); }

Create a new class inherited from LinearLayout, also override dispatchTouchEvent and onTouchEvent, and because LinearLayout inherited from ViewGroup, ViewGroup can intercept click events, this is easy to understand, because the controls are placed inside him. So you have to override the onInterceptTouchEvent method

@Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "onInterceptTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "onInterceptTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, "onInterceptTouchEvent ACTION_UP"); break; default: break; } return false; }

(There's a point here).

Here there is a place to note that some Android controls are clickable by default (such as Button), and some are clickable by default (such as TextView). Their distribution is different. Here we look at the clickable first. Create a new class to inherit android.support.v7.widget.AppCompatTextView, compatible TextView, and rewrite dispatchTouchEvent and onTouchEvent like Activity. The code is not pasted. Like above, it is a normal control with no interception method.

Start clicking, move finger and lift

Then click on the control on the screen to see the log printed, dispatchTouchEvent and onTouchEvent both return the default implementation super, onInterceptTouchEvent returns false by default, indicating no interception, default printing:

1.-------------------------------------- D/MainActivity: Activity dispatchTouchEvent ACTION_DOWND/MyLayout: dispatchTouchEvent ACTION_DOWND/MyLayout: onInterceptTouchEvent ACTION_DOWND/MyTextView: dispatchTouchEvent ACTION_DOWN2.-------------------------------------- D/MyTextView: onTouchEvent ACTION_DOWND/MyLayout: onTouchEvent ACTION_DOWND/MainActivity: Activity onTouchEvent ACTION_DOWN3.-------------------------------------- D/MainActivity: Activity dispatchTouchEvent ACTION_MOVED/MainActivity: Activity onTouchEvent ACTION_MOVED/MainActivity: Activity dispatchTouchEvent ACTION_MOVED/MainActivity: Activity onTouchEvent ACTION_MOVED/MainActivity: Activity dispatchTouchEvent ACTION_UPD/MainActivity: Activity onTouchEvent ACTION_UP

This is the default situation, and it is divided into three stages:

1. Event distribution, top to bottom, Activity to Layout to Text dispatchTouchEvent end

2. Event consumption, bottom to top, Text to Layout to Activity onTouchEvent end

3. Subsequent events of this set of actions are handed over to the last consumer of the previous event without distribution through other controls

Other return values of three functions

Both dispatchTouchEvent and onTouchEvent have three return cases

- true

- false

- super.dispatchTouchEvent(ev) and super.onTouchEvent(event)

onInterceptTouchEvent has two kinds of returns, true and false. In this way, there are really too many cases combined. If you write down and analyze the code one by one, I'm afraid you have to look dizzy, so here's a picture to see all the cases:

Event distribution process for normal non-clickable View

Event distribution for default clickable controls

For example, Button, a control that can be clicked by default, or a control that sets android:clickable="true", has some differences in the distribution process, mainly the default method of onTouchEvent, which directly consumes the click event and no longer passes up.

Clickable View event distribution flow

About "Android click event distribution mechanism principle analysis" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "Principle Analysis of Click Event Distribution Mechanism in Android." If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 211

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report