Android ontouchevent, onclick and Onlongclick call mechanism

Source: Internet
Author: User

For a view control on the screen, how does Android differentiate between triggering ontouchevent, onclick, or Onlongclick events?

In Android, a user action can be processed separately by different views and will fully respond to a user's UI action called Consuming the event (consume), so what sequence does Android deliver the event? And under what circumstances is it decided to consume the event?

Figuring out these issues is important for writing code that responds correctly to UI actions, especially when different view on the screen needs to respond differently to this UI action, a typical example of a user placing a widget on the desktop, So when the user is doing various things for the widget, the desktop itself sometimes responds to the user's actions and is ignored. Only by figuring out the mechanism of event triggering and passing will it be possible to ensure that UI controls still respond correctly to user actions in situations where the interface layout is very complex.

1. Ontouchevent

The 3 most common events to be handled in Ontouchevent are: Action_down, Action_move, action_up.

These three events identify the most basic user touch screen operation, the meaning is also very clear. Although people use them every day, but one thing please note that the Action_down event as the starting event, its importance is to exceed action_move and action_up, if the occurrence of action_move or ACTION_UP, Then there must have been a action_down.

There are some interactive mechanisms that can be seen in the Android source code based on this understanding of the different importance, as well as in the SDK, as in the ViewGroup onintercepttouchevent method, if the Action_ If True is returned in the down event, subsequent events will be sent directly to Ontouchevent instead of being sent to Onintercepttouchevent.

2. OnClick, Onlongclick and Ontouchevent

I've read a post that mentions that if you're working with ontouchevent in view, you don't have to deal with the onclick anymore because Android only triggers one of these methods. This understanding is not correct, for a certain view, the user completed a touch operation, obviously the signal from the sensor is the finger pressed and lifted two operations, we can understand as a click, it can also be understood that a action_down and action_up occurred, So how does Android understand and deal with it?

In Android, the onclick, onlongclick triggers are related to Action_down and action_up, and in time series, if we overwrite the onclick in a single view, Onlongclick and Ontouchevent, Ontouchevent is the first to capture Action_down and action_up events, followed by the possibility of triggering the onclick or onlongclick. The main logic is implemented in the Ontouchevent method in View.java:

Case Motionevent.action_down:

Mprivateflags |= PRESSED;

Refreshdrawablestate ();

if ((Mviewflags & long_clickable) = = long_clickable) {

Postcheckforlongclick ();

Break

Case motionevent.action_up:

if ((Mprivateflags & PRESSED)! = 0) {

Boolean focustaken = false;

if (isfocusable () && isfocusableintouchmode () &&!isfocused ()) {

Focustaken = Requestfocus ();

if (!mhasperformedlongpress) {

if (mpendingcheckforlongpress! = null) {

Removecallbacks (mpendingcheckforlongpress);

if (!focustaken) {

PerformClick ();

Break

As you can see, the Click Trigger occurs after the system snaps to action_up and executed by PerformClick (), and PerformClick invokes the OnClick () method of the previously registered listener:

public boolean PerformClick () {

if (Monclicklistener! = null) {

Playsoundeffect (Soundeffectconstants.click);

Monclicklistener.onclick (this);

return true;

return false;

The Longclick trigger is started from Action_down and is done by the Postcheckforlongclick () method:

private void Postcheckforlongclick () {

Mhasperformedlongpress = false;

if (mpendingcheckforlongpress = = null) {

mpendingcheckforlongpress = new Checkforlongpress ();

Mpendingcheckforlongpress.rememberwindowattachcount ();

Postdelayed (Mpendingcheckforlongpress, Viewconfiguration.getlongpresstimeout ());

As you can see, after the Action_down event is captured, the system starts triggering a postdelayed operation that triggers the execution of the checkforlongpress thread after the delay is 500ms,500ms on Eclair2.1:

Class Checkforlongpress implements Runnable {

public void Run () {

if (ispressed () && (mparent! = null)

&& Moriginalwindowattachcount = = Mwindowattachcount) {

if (Performlongclick ()) {

Mhasperformedlongpress = true;

If all the conditions are met, then execute Performlongclick () in checkforlongpress, and in this method Onlongclick () will be called:

public Boolean Performlongclick () {

if (Monlongclicklistener! = null) {

handled = Monlongclicklistener.onlongclick (view.this);

From the implementation, you can see that the onclick () and Onlongclick () methods are captured by the Action_down and action_up events and, depending on the circumstances, are ultimately determined to be triggered. In other words, if we listen to or overwrite the onclick (), the Onlongclick () and the Ontouchevent () method in an activity or view, it does not mean that only one of them will occur.

The following is a log of the basic timing of the OnClick trigger:

04-05 05:57:47.123:debug/tsactivity (209): OnTouch Action_down

04-05 05:57:47.263:debug/tsactivity (209): OnTouch action_up

04-05 05:57:47.323:debug/tsactivity (209): OnClick

It can be seen that the order of the onclick, action_up-Action_down, occurred.

The following is a log of the basic sequence of onlongclick that is triggered:

04-05 06:00:04.133:debug/tsactivity (248): OnTouch Action_down

04-05 06:00:04.642:debug/tsactivity (248): Onlongclick

04-05 06:00:05.083:debug/tsactivity (248): OnTouch action_up

You can see that the onlongclick is triggered after a certain amount of time has been pressed, and then the ACTION_UP is raised.

3. Can onclick and onlongclick happen at the same time?

To figure this out, just understand what Android's so-called consumption (consume) concept of event handling is, and a user's actions are passed to different view controls and different listening methods for the same control. Any method that receives and processes the event returns true after processing, and if the event is fully processed, the other view or listener method will not be able to process the event again.

The occurrence of Onlongclick is done by a separate thread, and before action_up, and the onclick occurs after action_up, so the same user touch operation may occur both Onlongclick and onclick. Isn't that incredible? So timely to the system to say "I have completely processed (consumption) of the user's this operation" is very important thing. For example, if we return true at the end of the Onlongclick () method, then the OnClick event has no chance of being triggered.

The following log is the basic timing of a touch operation in the case of the Onlongclick () method return false:

04-05 06:00:53.023:debug/tsactivity (277): OnTouch Action_down

04-05 06:00:53.533:debug/tsactivity (277): Onlongclick

04-05 06:00:55.603:debug/tsactivity (277): OnTouch action_up

04-05 06:00:55.663:debug/tsactivity (277): OnClick

As you can see, the onclick () method is still triggered after action_up.

Android ontouchevent, onclick and Onlongclick call mechanism

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.