Android ontouchevent, OnClick, Onlongclick call mechanism

Source: Internet
Author: User

In Android development, we often set the Onclick,onlongclick,ontouch event to a view, and sometimes we set these three events at the same time, so what is the order of execution when we set these three times?

First of all, the official documents on the face of Onlongclick () and Ontouch () are described below:

Onlongclick ()-This returns a, Boolean to indicate whether that has consumed the event and it should not being carried furthe R. That's, return true to indicate, which has handled the event and it should stop here; return FALSE if you had not handled it and/or the event should continue to any other on-click listeners.
The general meaning is: the Onlongclick () method returns a Boolean value that, if true, indicates that the event has been processed, does not need to continue passing, and if False, indicates that the event will continue to other listener events.
OnTouch ()-This returns a Boolean to indicate whether your listener consumes this event. The important thing is, this event can has multiple actions, that follow, each of the other. So, if you return False when the ' Down ' action event is received, you indicate the. Consumed the event and is Also not interested in subsequent actions from the this event. Thus, you are not being called for any other actions within the event, such as a finger gesture, or the eventual up action E Vent.
The general meaning is: the OnTouch () method, which contains multiple behaviors, aciton_down,action_move,action_up. If True at the return value, the event is handled, the onclick or onlongclick is no longer invoked, and if False is returned, the Onlongclick continues to be called.

Write a simple test procedure:

Package Com.qwei.demo;import Android.app.activity;import Android.os.bundle;import android.util.log;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onlongclicklistener;import Android.view.view.ontouchlistener;import Android.widget.TextView; public class Mainactivity extends Activity {private final static String TAG = MainActivity.class.getName ();p rivate Tex    TView Clickme,showlog;    Private String log;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        ClickMe = (TextView) Findviewbyid (R.id.click_me);        Showlog = (TextView) Findviewbyid (R.id.show_log); Clickme.setonlongclicklistener (New Onlongclicklistener () {@Overridepublic Boolean onlongclick (View arg0) {//TODO                  auto-generated method Stub log.d ("TAG", "Onlongclick is called ....."); Log = log + "Onlongclick is called......... \ n ";          Showlog.settext (log); return false;}         }); Clickme.setonclicklistener (New Onclicklistener () {@Override public void OnClick (V                  Iew v) {log.d ("TAG", "OnClick is called .....");                Log = log + "OnClick is called.............\n";            Showlog.settext (log);         }          }); Clickme.setontouchlistener (New Ontouchlistener () {@Override public boolean Ontouc                  H (View arg0, motionevent arg1) {//log.d ("TAG", "OnTouch is called ..."); Switch (arg1.getaction ()) {case MOTIONEVENT.ACTION_DOWN:LOG.D ("TAG", "Action_down.                      ............");                    Log = log + "oaction_down............\n";                    Showlog.settext (log);                  Break Case MOTIONEVENT.ACTION_MOVE:LOG.D ("TAG", "Action_move .....".);                    Log = log + "action_move.............\n";                    Showlog.settext (log);                  Break                      Case MOTIONEVENT.ACTION_CANCEL:LOG.D ("TAG", "Action_cancel .....");                    Log = log + "action_cancel............\n";                    Showlog.settext (log);                  Break                      Case MOTIONEVENT.ACTION_UP:LOG.D ("TAG", "action_up .....");                    Log = log + "action_up............\n";                    Showlog.settext (log);                  Break                  Default:break;              } return false;      }          }); }}
When Ontouch returns False,onlongclick also returns false, long pressing view, call Ontouch, call Onlongclick after a period of time, lift the finger when the onclick is called, as shown in:

When Ontouch returns False,onlongclick also returns True, when the view is long pressed, the Ontouch is called, Onlongclick is called after a period of time, and the onclick is not invoked when the finger is lifted, such as:


When Ontouch returns True, the call to Ontouch,onlongclick and the onclick will not be invoked when the view is long pressed, such as:


Android ontouchevent, OnClick, 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.