The sequence of calls for these three events is:
Ontouch->onlongclick->onclick
Let's look at the handling functions of these three events:
- public boolean OnTouch (View V, motionevent event);
- public boolean Onlongclick (View v);
- public void OnClick (View v);
See the return values of three functions, only the onclick is void, and Ontouch and Onlongclick are Boolean, because the system handles these events conditionally and must satisfy the condition to trigger the corresponding event handler.
Events are passed through the chain of events, and if the level is not processed, it is passed down to the next level until the entire chain of events is propagated.
Ontouch returns true to indicate that the event has been processed, that the event is not continued to be passed in the event chain, and that if the Onlongclick returns true, the table name handles the event and does not continue to pass the event in the event chain.
If you want the OnClick event in the view to coexist with the Ontouch event, and the two events are triggered individually on a single button, you can simply remove the onclick event and subdivide it directly in the Ontouch event, if the position is pressed and the position is lifted, i.e. startx== LASTX, the OnClick event is considered to be the following code:
@Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:{//record the x-coordinate of the pressed pointStartX =event.getrawx (); Break; } Casemotionevent.action_up://record the x-coordinate of the raised point, if two times equal, trigger the Click event, otherwise it is a sliding event floatLASTX =event.getrawx (); if(StartX = =lastx) {Toast.maketext (Getapplication (),"Clicked", Toast.length_short). Show (); }Else{Toast.maketext (Getapplication (),"Slide.", Toast.length_short). Show (); } Break; Casemotionevent.action_move:{ Break; } } return false; }
Ontouch and onclick and Onlongclick events for Android view