Android Learning Note: Two types of event handling for Android

Source: Internet
Author: User

Android provides two ways to handle event handling: Callback-based event handling and listener-based event handling.

Easy for us to understand a little:

(1) callback-based event processing is the inheritance of GUI components and overrides the event handling method of the component. In most cases, the use of callback-based event handling is not the best option, except in some specific cases.

(2) listener-based event processing is the process of defining an event in the listener, and then binding the listener to the component.

Such a way is undoubtedly more able to embody the object-oriented thinking.

Callback-based event handling:

the View class includes, for example, the following methods that can be overridden:

Boolean onKeyDown (int keycode,keyevent event): Triggered when a key is pressed

Boolean onKeyUp (int keycode,keyevent event): Triggers When a key is released

Boolean onkeylongpress (int keycode,keyevent event): Triggered when a key is long pressed

Boolean onkeyshortcut (int keycode,keyevent event): Triggered when a shortcut key is pressed

Boolean ontouchevent (Motionevent event): triggered when touching the screen

We notice that the return value is a boolean value. This is used to identify whether the method can handle the event completely. Returns true to indicate that the event is not propagated, that it is not fully processed, that events are propagated. The following event handling methods are given to handle this.


Listener-based event handling:

This is a delegated way of handling events: a common component (the event source) trusts the event handler to the (bound) event listener, when the event source has the specified event. Notifies the event listener to handle the event.

A monitoring-based event processing flowchart is shown:


Learn the following concepts:

Event Source: is a variety of GUI components.

Event Listener: Various xxxlistener classes. Interfaces that need to implement the event-handling method.

Register listener: Invokes the Setxxxlistener (Xxxlistener) method of the event source .

The View includes, for example, several internal interfaces:

View.onclicklistener: Handling Click events

View.oncreatecontextmenulistener: Handling events that click a context menu item

View.onfocuschangelistener: Handling Focus Change Events

View.onkeylistener: Handling Key Events

View.onlongclicklistener: Handling Long Press events

View.ontouchlistener: Handling Touch Events

An event listener can be shared by multiple GUI components.

Implementing event listeners can have several forms, such as the following:

Inner classes, external classes,Activity itself implementations, anonymous inner classes, directly bound to tags (this is a special but very convenient form).

This implementation is not difficult to implement if you are familiar with Java.

Android Learning Note: Two types of event handling for Android

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.