14th-day Android Touch event Learning 1 Click Event

Source: Internet
Author: User

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

Android Touch event Learning Series Summary

Touch events are frequently encountered during Android development, especially when customizing UI controls. I have been learning and summarizing events, learning the event transmission mechanism, recognizing gestures, intercepting, and distributing events. I also wrote some summaries, but they all felt messy, without a system, there is no clue that everything is just a little east. Of course, I also checked a lot of information and read some books and customized several controls, when sorting out the previous summary, I suddenly thought of summing up the Android Touch events from the process of discovering and learning a little bit after I got started with the Android events, you can systematically sort out the learned knowledge to identify missing makeup, and also help you discover whether there are cognitive errors.

First, let's list the knowledge that individuals have come into contact with learning events:
1. Various event setting methods and interfaces (for example, setOnClickListener)
2. Principles of various events (onTouchEvent and MotionEvent parameter passed by the method)
3. Various gestures (for example, rolling, Fling, and touch)
4. dispathTouchEVent)
5. onInterceptTouchEvent)
6. Event Transfer Mechanism

Let's take a look at the first knowledge point of contact, and summarize the rest. After writing Hello World at the earliest, the learning and practice demo is onClickListener. Setting a button to click will trigger the code. Usually, a Toast is displayed or a LOG is printed. The following describes how to start an event learning journey from a simple Demo at the same level as Hello World.

1. Click Event example

@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); View view = getWindow (). findViewById (R. id. touch_one); view. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, "Click image", Toast. LENGTH_SHORT ). show ();}});}

There are few lines of a small demo code. Let's take a look at the following:

Call View. setOnClickListener and pass an internal class of View. OnClickListener. A Toast text "Click image" is displayed in one sentence ".

As shown in the preceding figure, the "Click image" text will pop up when you click an image in the middle, indicating that when you click on the view using setOnClickListener, the code of its internal class will be triggered.

Ii. View event sorting

First, I learned a click event, and then checked which event-related methods are provided by Andorid View. These methods all start with setOn, view files on the official website or in Eclipse or other IDE. the following methods are displayed for setOn:


There are 6 related methods, the first one is the setOnClickListener used in the current example. It can be seen from the name that setOnLongClickListener is a long-press event listener and setOnTouchListener is a Touch event listener, only these three methods are related to events in terms of names.

Click Event trigger, and then analyze how the Android source code triggers the execution of the setOnClickListener internal class and how to determine the click behavior of the current operation?

In the next article Touch event Learning 2 where to trigger a click event, you can find the click event where to trigger execution from the Andorid source code perspective.

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.