The event handling mechanism of Android:
Listener-based event handling
Callback-based event handling
Listening:
Bind specific event listeners for Android components, which are invoked when the corresponding event is triggered;
Three objects:
1. The event source is the place where the event occurs, usually the individual components
2. Events
3. Event Listener
Four ways to listen to events:
When the control calls Setonclicklistener (), it needs to pass in a onclicklistener parameter. By looking at the source code, we will find that Onclicklistener is an interface.
So, we can consider three ways to implement the functionality of this interface:
1. Create an external class that implements the interface:
Cases:
publicclass ClickOkListener implements OnClickListener{ //重写OnClick方法}//**************************************////在Activity中调用btn.setOnClickListener(new ClickOkListener)
2. How anonymous internal classes are
Cases:
btn.setOnClickListenerOnClickListener(){ onClick() { //处理事件 }});
3.Activity as Event listener
(1). Btn.setonclicklistener (This)
(2). Let activity implements Onclicklistener
(3). Overriding the OnClick method in activity
In addition, Android allows developers to bind the handling of a point-and-click event by setting the OnClick property in XML.
Callback:
Handles Android component-specific callback methods.
In the callback's event-handling model, the event source and event listener are unified, and when the user triggers the event, the component's own specific method handles the event.
Common callback methods in the view class:
The OnKeyDown button is pressed
Onkeylongpress button is long pressed
OnKeyUp Button is released
Ontouchevent Triggering touch screen events
Action steps for callback events:
1. Custom control Classes
2. Overriding the callback method of the parent class in the custom control
3. In the XML layout file, use the custom control directly
Add: Almost every event-handling method has a Boolean-type return value that indicates whether the processing method fully handles the event.
If true, the event is handled and the event is not passed.
If False is returned, the event is not handled and the event is passed down.
Sequence of event handling:
1. Monitoring
2. Callbacks
3. The current Activity
Copyright notice: Just out of the original content of the pot, I hope you have help ~
Monitor and callback for event handling of Android