The relationship between Ontouch and the OnClick event in Android

Source: Internet
Author: User

These days encountered a bit about Android touch events related, but also with the onclick, write down:

LinearLayout set the Ontouchlistener,onclicklistener,onlongclicklistener and ontouchevent callbacks respectively


1. After touching the screen, the basic execution flow is as follows:

Ontouch,action=0
Ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Ontouch,action=1
Ontouchevent,action=1
OnClick

That is to say Ontouchlistener is the first to be triggered, then the ontouchevent callback itself, when the last up event occurs and is ontouchevent processed, the Onclicklistener is triggered.


2, the ontouchevent callback in the super.ontouchevent removal, directly return true; The process is as follows:

Ontouch,action=0
Ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Ontouch,action=1
Ontouchevent,action=1

You can see that the Onclicklistener will never be triggered, and that the trigger on the onclick is implemented in the Ontouchevent callback.


3, long press the screen, the process is as follows:

Ontouch,action=0
Ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Onlongclick
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Ontouch,action=1
Ontouchevent,action=1
OnClick


When a long press is pressed, the onlongclick response is triggered without going to up, but the onclick response continues to be triggered afterwards.


4, but if True is returned in Onlongclick, then the process is as follows, that is, the onlongclick is triggered only in the middle, and then continues to respond to touch, but the onclick is no longer triggered when up

Ontouch,action=0

Ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Onlongclick
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
Ontouch,action=1
Ontouchevent,action=1




The test code is as follows:

public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s        Uper.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Framelayout frame = (framelayout) Findviewbyid (R.id.container);        Linearlayouttest line = new Linearlayouttest (this);        Line.setonclicklistener (New Clicklistener ());        Line.setonlongclicklistener (New Longclicklistener ());        Line.setontouchlistener (New Touchlistener ());        Line.setlongclickable (TRUE);    Frame.addview (line);            } public class Clicklistener implements Onclicklistener {@Override public void OnClick (View v) {        LOG.E ("Test", "OnClick"); }} public class Longclicklistener implements onlongclicklistener{@Override public boolean onlongclic            K (View v) {log.e ("test", "Onlongclick");        return true;      }} public class Touchlistener implements ontouchlistener{  @Override public boolean OnTouch (View V, motionevent event) {LOG.E ("test", "ontouch,action=" +event.get            Action ());        return false; }     }        }

public class Linearlayouttest extends linearlayout{public    linearlayouttest (context context) {        Super (context) ;    }    @Override Public    Boolean ontouchevent (Motionevent event) {        log.e ("Test", "ontouchevent,action=" + Event.getaction ());        Return Super.ontouchevent (event);}    }




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.