Android Callback-based event handling----event distribution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.