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.

It's easy for us to understand:

(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 choice except for some specific situations.

(2) listener-based event processing is the process of defining an event in the listener, and then binding the listener to the component. This way is undoubtedly more able to embody the object-oriented thinking.

Callback-based event handling:

the View class contains the following methods that you can use to override:

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 that identifies whether the method can handle the event completely. Returns true to indicate that the event is not propagated, that the events are not fully processed, and that the events are propagated. The following event handling method is given to handle.


Listener-based event handling:

This is a delegated way of handling events: an ordinary component (the event source) Delegates the event handler to the (bound) event listener, and notifies the event listener when the event source has the specified event to handle the event.

A monitoring-based event processing flowchart is shown:


Here are some concepts to understand:

Event Source: is a variety of GUI components.

Event Listener: A variety of Xxxlistener classes that require an interface to implement event-handling methods.

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

The View contains several internal interfaces as follows:

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.

There are several ways to implement event listeners:

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

These implementations are 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.