There are two types of Android event handling mechanisms:
Listener-based event handling
Callback-based event handling.
Listen-based must have learned Java or touch Android know, recently read about the callback aspect of the book, today is mainly to talk about callback-based event processing mechanism. More for my own understanding of the summary and reference to some other people's things, there are mistakes or do not understand the place can be pointed out in the comments, thank you ~
procedure based on callback event handling:
To re-select the Android component-specific callback method, or rewrite the activity callback method, most Android interface components provide an event response callback method, developers just rewrite it. , General, callback-based event processing can be used with general-purpose events, and its code is concise.
First, callback mechanism and monitoring mechanism
The event source is unified with the event listener for the callback event-handling model, meaning that the component's own specific method is responsible for handling the event when the user fires an event on the GUI component. In order to use the callback mechanism class to handle events that occur on a GUI component, we need to provide the corresponding event handling for that component, but Java is a static language, and we cannot dynamically add methods to an object, only inherit the GUI component class, and then override the class's event-handling method to implement it.
to implement event handling for the callback mechanism, Android provides a number of callback methods for event handling for all GUI components. take view As an example, this class contains the following methods:
boolean onKeyDown (int keycode,keyevent event): This method is triggered when a user presses a key on the component.
boolean onkeylongpress (int keycode,keyevent event): This method is triggered when a user presses a key on the component long.
boolean onkeyshortcut (int keycode,keyevent event): Triggers the method when a keyboard accelerator event occurs.
boolean onKeyup (int keycode,keyevent event): This method is triggered when a user releases a key on the component.
boolean ontouchevent (Motionevent event): This method is triggered when a user triggers a touchscreen on that component.
boolean ontrackballevent (Motionevent event): This method is triggered when the user tracks a ball-screen event on the component.
Second, callback-based event propagation (event distribution mechanism)
Third, override the Ontouchevent method to respond to touch screen events
This article is from the "unity of Knowledge" blog, please be sure to keep this source http://poarry.blog.51cto.com/5970996/1554560
Android Callback-based event handling----event distribution