Blog Source: http://blog.csdn.net/sinyu890807/article/details/9097463
There is currently a very simple project with only one activity, and there is only one button in the activity. You may already know that if you want to register a click event with this button, you only need to call:
Button.setonclicklistener (new Onclicklistener () { @Override publicvoid onclick (View v) { log.d ("TAG""onclick Execute"); } });
Button.setontouchlistener (new Ontouchlistener () {@Override public boolean OnTouch (View V, motionevent event " tag " , ontouch Execute, Action " + event .getaction ()); return false ; } });
The Ontouch method can do more things than the onclick, such as judging the finger presses, lifts, moves and other events. So if I have two events registered, which one will be executed first? Let's try it out, run the program click on the button and print the results as follows:
As you can see, Ontouch takes precedence over the onclick execution, and Ontouch executes two times, one is Action_down, one is action_up (and there may be several action_move executions, if you shake your hand a bit). So the order in which the events are passed is Ontouch first and then passed to the onclick.
The Ontouch method has a return value, and here we return false, if we try to change the return value in the Ontouch method to the True,onclick method, it is no longer executed. We can regard this as a touch event that was intercepted by the OnTouch method and did not continue to be passed to the onclick.
So how does the click event of a View pass? :
The source code of Dispatchtouchevent is as follows:
Public Event ) { ifnull && (mviewflags & enabled_mask) = = ENABLED && Montouchlistener.ontouch (Thisevent)) { returnTrue ; } return ontouchevent (event); }
Android Learning event distribution mechanism