15th-day Android Touch event Learning 2 where the click event is triggered

Source: Internet
Author: User
Tags android sdk manager


For more information about the touch event Learning Series, see:

Android Touch event Learning Series Summary


This article discusses Touch event Learning 1 Click Event on the basis of the previous article. However, the example is very simple and just an introduction, the first step in event Learning is usually to learn the first event-related example of Android development. Now let's analyze the Click Event principle from the perspective of Andorid source code. The source code here is based on Android 4.0 (that is, Andorid 14 ).

I have also compiled the Android source code to generate a zip file and fl it to my mobile phone. However, after I reinstalled the system source code, I didn't keep it all formatted, and it is time-consuming to completely download the Andorid source code, the most important thing is that there is no need. Android 4.0 is used here because the Android SDK Manager can easily obtain the source code of this version by checking "Sources for Andorid SDK, because Android 4.0 provides the lowest version, this version is used for analysis. If you want to save some trouble, you can directly through the online source code, address is: http://grepcode.com/Direct Search android can be online to view all versions of Android source code and can be compared, however, I personally prefer to view local code through IDE.


1. Click Event-related source code tracking

1. From the example in the previous article, we can see that only one method is called, namely view. setOnClickListener. Here we will start to View it from the setOnClickListener method of the view class.

/*** Register a callback to be invoked when this view is clicked. if this view is not * clickable, it becomes clickable. ** @ param l The callback that will run ** @ see # setClickable (boolean) */public void setOnClickListener (OnClickListener l) {// first determine whether you can click if (! IsClickable () {// if you cannot click, reset it to setClickable (true);} // copy the internal class parameter to the internal variable mOnClickListener = l ;}



2. Where to call mOnClickListener?

As shown above, setOnClickListener initializes the internal variables of mOnClickListener. Where can it call mOnClickListener? You can find in the View class

/*** Call this view's OnClickListener, if it is defined. ** @ return True there was an assigned OnClickListener that was called, false * otherwise is returned. */public boolean initiate mclick () {// send a specified type of clickable event. SendAccessibilityEvent (AccessibilityEvent. TYPE_VIEW_CLICKED); if (mOnClickListener! = Null) {// playSoundEffect (SoundEffectConstants. CLICK); // * emphasis * triggers the onClick method mOnClickListener in the internal class. onClick (this); return true;} return false ;}

Now I have found out which code executes the onClick method of the setOnClickListener internal class. Where can I call the javasmclick function to trigger the execution click event? Then, you can use javasmclick to find out where this method is called.


3. Where is the call execution click event?

By searching for javasmclick in the View class, you can find that onKeyUp and onTouchEvent are called at two places. If there are some Android basics, it can be seen that onKeyUp usually clicks the physical key (for example, the back key and the Home key) the previous example is triggered by a finger click. Therefore, the onKeyUp rule is excluded.


4. Click the event trigger source onTouchEvent

/*** Implement this method to handle touch screen motion events. ** @ param event The motion event. * @ return True if the event was handled, false otherwise. */public boolean onTouchEvent (MotionEvent event ){...... if (viewFlags & CLICKABLE) = CLICKABLE | (viewFlags & LONG_CLICKABLE) = LONG_CLICKABLE) {switch (event. getAction () {case MotionEvent. ACTION_UP :...... // click the final processing location of the event if (mPerf OrmClick = null) {mPerformClick = new custom mclick ();} if (! Post (mPerformClick) {initialize mclick () ;}...... break;} return true ;}return false ;}


The above only posts the code associated with onTouchEvent and click event, because there are too many code in this method to avoid interference and only a few lines of core pseudo code are pasted. We can draw a simple conclusion that the onTouchEvent calls the click event to execute the code.


2. Analyze the Click Event Process

The above code is found by checking the source code in the lower-right corner and step by step to find out where the click event is triggered, but the Code must be executed from top to bottom during actual execution, in addition, it is easy to understand the analysis according to the execution process, but there may be another question: where to call and execute the onTouchEvent? It is a good habit to discover and raise questions, but here we know that onTouchEvent is the final place to handle click events. Learning is a little bit. We will summarize this question later, this issue should be put on hold first.

Now, from the analysis of the source code, it is also the code execution process.

1. If you click the screen with your finger, the Android system will first trigger the onTouchEvent method.

2. From case MotionEvent. ACTION_UP, we can see that the performClick () method is triggered when the finger is lifted.

3. mOnClickListener. onClick (this) will be called in the merge mclick method );

4. The mOnClickListener variable is initialized in the setOnClickListener method.

5. Trigger the execution of the Code in the setOnClickListener internal class. In the previous example, the "Click image" text is displayed.


Of course, here is just a brief introduction of the click event process, which is not described in detail. The purpose is to first display the final method of event processing View. onTouchEvent ..


Iii. Other event principles and onTouchEvent Method

If you want to learn more, you can refer to the method of event stakeholders provided by View in the previous article.

setOnClickListenersetOnLongClickListener  setOnTouchListener  
Only the setOnClickListener method is tracked here. If you are interested, you can track where the other methods are called.


It doesn't matter if you are too lazy and don't want to check it yourself. The above three methods are eventually processed in onTouchEvent. Therefore, this method is the final method for Android to process all events, the click events and long-pressed events shown above, as well as various gestures that will be learned later (for example, rolling, Fling, and touch), are used to determine gestures in this method.






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.