Android Development detailed Ontouch and onclick

Source: Internet
Author: User

Sequence and association of Ontouch,onclick,onlongclick events in Android components:

One, Ontouch returns false


The first is the Ontouch event of the down event, at this time, if the long press, triggering the Onlongclick event;
Then the up event of the Ontouch event occurs, the up is complete, and the onclick event is finally triggered.


Two, Ontouch returns True


The first is the down event of the Ontouch event, then the up event of the Ontouch event, and the onclick and Onlongclick events do not occur during the period.


Three, Ontouch:down return True,up return false: The result is the same as two.


Mechanism Analysis:
Ontouch event: The down event return value marks whether this event is a click event (returns False, is a click event, returns True, not recorded as a click event), and the up event marks the end time of this event, which is to determine whether it is a long press.
As long as the down returns TRUE, the system will not record this event as a click event, nor will it trigger the onclick or Onlongclick event. Therefore, the system will not continue to trigger the OnClick event even if it returns false when up.


Four, Ontouch:down returns false,up true:


The first is the down event of the Ontouch event, at this point:
Long press, trigger the Onlongclick event, and then the up event of the Ontouch event occurs, complete.
Short press, first trigger Ontouch up event, after a certain time, automatically trigger Onlongclick event.
Mechanism Analysis:
Ontouch event: The down event return value marks whether this event is a click event (returns False, is a click event, returns True, not recorded as a click event), and the up event marks the end time of this event, which is to determine whether it is a long press.
When down returns false, marking this event as a click event, and up returning true indicates that the event has not ended, that is, it has been pressed long, and has reached a long press critical time, and the OnClick event has not been triggered to

The relationship between each view of Android is a tree, and the following sub-view is created on the basis of the parent view, which can be viewed as a child view superimposed on the view of the parent class. Let's take a look at two different cases of event handling.
1, to the same view set the Ontouchevent event, Onclicklistener event and Onlongclicklistener event, when clicking on the view, the response order of the event is:
Touch event Action_down, Action_up, Onclick/onlongclick.


How does the view respond to this click action?


First, the view responds to the Action_down event and returns a Boolean value, where there are two kinds of judgments:
A: Returns True, indicating that the view accepts this press action, which means that the click action is aborted and then responds to the Action_up event. Clicking on the action is Action_down accepted, so the subsequent Onclick/onlongclick event will not respond.
B: Return false, indicating that the view does not accept this press action, after the response, the press continues to send down, followed by the response to the Action_up event, here is another judgment:
If the Action_up event returns True, the ACTION_UP accepts the release action, the release action is aborted, the view is pressed, and the view responds to the Onlongclick event.
If the Action_up event returns false, the ACTION_UP does not receive the release operation and the release continues to be issued, because the press and release actions are not aborted, so the view responds to the OnClick event.
2. Event response between multiple view:
If you have a multi-layered view, a layout, put a textview on this layout, and set the TextView's width to fill_parent (make sure the click is on the TextView). Set Ontouchlistener events for both activity and TextView. The Order of the event responses is first from the top-level view. So, when you tap the screen (the screen in the TextView area).
TextView responds Action_down, returns False,action_down continues to be issued, activity responds to Action_down events. If true, it means that action_down to TextView is aborted without continuing to be captured by the activity.
The response of Action_up is somewhat different, and in response to action_up there are two conditions that must be met:
A, the bottom layer (here is activity).
b, its corresponding action_down is the terminating point, that is, the return value of its action_down is true;

Android Development detailed Ontouch and onclick

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.