"Android App Development technology: Application Components" Android event handling mechanism

Source: Internet
Author: User

Guo Xiaoxing
Weibo: Guo Xiaoxing's Sina Weibo
Email:[email protected]
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwells

Event handling: Whether a desktop application or a mobile app needs to respond to a user's actions, the mechanism that responds to user actions is event handling.

Android provides two sets of event handling mechanisms, as follows:

    • Listener-based event handling
    • Callback-based event handling

In general, callback-based event processing can be used to handle some common events, but only listening-based event handling can be used for specific events. And listen-based event handling is prioritized.

Let's take a detailed description of these two event handling methods.

A listening-based event processing

Listener-based event processing is a delegated event-handling mechanism, in which an ordinary component (event source) delegates the entire event handler to a specific object (the event listener), notifies the event listener that is being delegated when the event source has a specified event, and the event listener handles the event.

1.1 Monitoring-based event processing model

The listener-based event processing model contains the following 3 classes of objects:

    • Event sources: The event source, the place where events occur, usually the components.
    • Event: event, which encapsulates the specific thing that occurs with the interface component, is usually a user-related action, and is typically obtained through an event object if the program needs to obtain information about the events released on the interface component.
    • Event Listener: That is event Listener, which is responsible for monitoring events that occur at the source of events and responding to various events.

The listener-based event processing model looks like this:

Monitoring-based event processing model Visio source file download

1.2 Event Source Listener

The listener-based event-handling programming steps are as follows:

    1. Gets the normal interface component (event source), which is the object being monitored.
    2. Implements the event listener class, which implements an Xxxlistener interface.
    3. The Setxxxlistener method that invokes the event source registers the event listener object with the normal component.

Example

Define a button as the event source

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:orientation  =" vertical " android:layout_width  = "fill_parent"  android:layout_height  = "fill_parent"  android:gravity  =; <EditTextandroid:id= "@+id/show"android:layout_width="Fill_ Parent "android:layout_height=" Wrap_content "android:editable=" false " / >                   <buttonandroid:id="@+id/bn"android:layout_width="Wrap _content "android:layout_height=" Wrap_content "android:text=" click Me " />                   </linearlayout>

Implements listeners to listen for event sources.

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;//Implement Event listener interface Public  class activitylistener extends Activity implements  Onclicklistener{    EditText Show; Button bn;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main);        Show = (EditText) Findviewbyid (r.id.show); bn = (Button) Findviewbyid (R.ID.BN);//Use activity directly as an event listenerBn.setonclicklistener ( This); }//Implement event handling methods    @Override     Public void OnClick(View v) {Show.settext (the BN button was clicked! "); }}

In the listener-based event processing model contains the following 3 types of objects, event sources are ubiquitous, the event is generated by the Android system, so we need to focus on the implementation of the event listener, there are 4 common event listeners, as follows:

    • Inner class form: Defines an event listener as an inner class of the current class.
    • External class Form: Defines an event listener as an external class.
    • Activity itself acts as an event listener: Let activity itself implement listener interfaces and implement event handling methods.
    • Anonymous inner class form: Creates an event listener object using an anonymous inner class.
Two callback-based event handling

Callback-based event handling does not have an event listener, and the component's own specific method is responsible for handling the event when the user fires an event on the interface component.

2.1 Callback-based event propagation

Almost all callback-based time-processing methods have a Boolean-type return value that identifies whether the processing method can handle the event fully.

    • If the callback method that handles the event returns true, indicating that the processing method is fully capable of handling the event, the event is not propagated.
    • If the callback method that handles the event returns false, indicating that the processing method does not fully handle the event, the event is propagated.

    • -

Copyright NOTICE: When we seriously to do one thing, we can find the endless fun, colorful technology like the scenery on the road, while walking to appreciate.

"Android App Development technology: Application Components" Android event handling mechanism

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.