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 !!!