Click the differences between the event and the touch event, and click the event touch

Source: Internet
Author: User

Click the differences between the event and the touch event, and click the event touch

For a View component on the screen, how does Android differentiate whether an onTouch event or an onClick event should be triggered? In Android, a user operation can be processed by different View components in order, and a UI operation that completely responds to the user is called a consume event ), in what order does Android send events, and under what circumstances does it determine that the event is consumed? The following is a specific piece of code.

In this Code, add a click event listener for the button, and output onClick (Click Event) through the Log. I () method ). Then, add a touch event for the button and determine whether the current finger is pressed or lifted. The Code is as follows:

01 Button button = (Button) findViewById (R. id. btn); // get the button object 02 // Add and click event listener 03 button for the button. setOnClickListener (new View. onClickListener () {04 @ Override05 public void onClick (View v) {06 Log. I ("onClick", "Click Event"); 07} 08}); 09 // Add a touch event listener 10 button for the button. setOnTouchListener (new View. onTouchListener () {11 @ Override12 public boolean onTouch (View v, MotionEvent event) {13 if (event. getAction () = MotionEvent. ACTION_DOWN) {// indicates 14 logs when the finger is pressed. I ("onTouch", "press"); 15} else if (event. getAction () = MotionEvent. ACTION_UP) {// indicates 16 logs when the finger is lifted. I ("onTouch", "lift"); 17} 18 return false; // This event is not consumed 19} 20 });

After executing the code above, click the button on the screen and the result shown in 7.11 is displayed in the LogCat panel.

 

Figure 7.11 execution sequence

Note: When you set a click event and a touch event for a component at the same time, the touch event is executed first and then the click event is executed. When the return value of the touch event listener is true, this event is consumed and no click event is executed.

 

This article is excerpted by Li Lei, author of "zero basic learning Android" published by tomorrow's technology. For more information, see the source !!!

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.