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:
[Java]View Plaincopyprint?
- Public class Mainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.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 Onlongclick (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.getaction ());
- return false;
- }
- }
- }
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; } } }
[Java]View Plaincopyprint?
- 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);
- }
- }
The relationship between Ontouch and the OnClick event in Android