Today's CSDN public class on Android event handling problems Fast Mastering the teacher spoke about a concept I don't think is right.
The exact words are: Click events can be implemented through both event monitoring and callback methods .
As soon as I heard it, my expression was this:
This is not the same as what I learned, it is also a monitoring and callback. This time I immediately ask a question, well, the lecture teacher saw.
The teacher said: "Is not the same, we 4th (last one o'clock) will speak."
Well, I sat ready to be taught to listen for half an hour. The last thing I heard was the expression:
That's enough nonsense, a little joke, don't take offense.
As we all know, there is only one kind of listener event, which is callback. What if someone doesn't understand what a callback is? Can look at the beginning of one of my articles in the example: callback study notes
We take the button as an example, the button is the subclass of view this is undoubtedly, so we directly look at the source in view:
Public void Setonclicklistener (@Nullable onclicklistener l) { if (! Isclickable ()) { setclickable (true); } = l; }
This is simple, just to save a onclicklistener of a specific subclass. When we click on the button, the system will definitely callback ontouchevent this method, and in view this method is this:
1 Public Booleanontouchevent (Motionevent event) {2 Final floatx =event.getx ();3 Final floaty =event.gety ();4 Final intViewFlags =mviewflags;5 Final intAction =event.getaction ();6 7 //code omitted8 9 if((viewflags & clickable) = = Clickable | |Ten(ViewFlags & long_clickable) = = long_clickable) | | One(ViewFlags & context_clickable) = =context_clickable) { A Switch(action) { - Casemotionevent.action_up: - Booleanprepressed = (Mprivateflags & pflag_prepressed)! = 0; the if((Mprivateflags & pflag_pressed)! = 0 | |prepressed) { - BooleanFocustaken =false; - if(Isfocusable () && isfocusableintouchmode () &&!isFocused ()) { -Focustaken =Requestfocus (); + } - + if(prepressed) { ASetpressed (true, x, y); at } - - if(!mhasperformedlongpress &&!)mignorenextupevent) { - if(!Focustaken) { - - if(Mperformclick = =NULL) { inMperformclick =NewPerformClick (); - } to if(!post (Mperformclick)) { + PerformClick (); - } the } * } $ Panax Notoginseng if(Munsetpressedstate = =NULL) { -Munsetpressedstate =Newunsetpressedstate (); the } + A if(prepressed) { the postdelayed (Munsetpressedstate, + viewconfiguration.getpressedstateduration ()); -}Else if(!post (munsetpressedstate)) { $ Munsetpressedstate.run (); $ } - - Removetapcallback (); the } -Mignorenextupevent =false;Wuyi Break; the - CaseMotionevent.action_down: Wu //code omitted - Break; About $ CaseMotionevent.action_cancel: - //code omitted - Break; - A CaseMotionevent.action_move: + //code omitted the Break; - } $ the return true; the } the the return false; -}
Here I delete a lot of code, we know that a click is composed of a action_down, a number of action_move and a action_up, that is, when the click is complete, the code in the ACTION_UP will be executed. In line 32nd, calling a method called PerformClick, this method is to execute the Click event directly, we see the inside of this method:
1 Public BooleanPerformClick () {2 Final Booleanresult;3 FinalListenerinfo Li =Mlistenerinfo;4 if(Li! =NULL&& Li.monclicklistener! =NULL) {5 Playsoundeffect (Soundeffectconstants.click);6Li.mOnClickListener.onClick ( This);7result =true;8}Else {9result =false;Ten } One A sendaccessibilityevent (accessibilityevent.type_view_clicked); - returnresult; -}
In line 6th, we call the OnClick method in the implementation class of the interface we passed in, which is the callback, because this is not a real call.
Summary: No matter what kind of listening events, they are implemented by callbacks. After we click, the event is captured by the system, the underlying control is progressively uploaded to the control, and then callback to the view's Ontouchevent method, which callbacks the OnClick method to the interface we set. I know that callbacks and snooping are different, but they're definitely not two methods.
Android Development Learning Path-one time CSDN public class