Android Event handling

Source: Internet
Author: User

1. Event handling Overview and Android event handlingEvent handling: When the user performs various actions on the program interface, the application must provide actions for the user's actions, which need to be done through event handling
Two kinds:    1. Listener-based event handling (priority Trigger)Method: Bind a specific event listener Setonclicklistener.                 The Android:onclick property of the UI component is also allowed to specify the event listener method and then define the event listener method in the activity.       Eg:android:onClick = "ClickHandler" defines a ClickHandler (View source) method in the activity that will handle the Click event on the button. The processing model of event listener mainly involves the following three kinds of objects:->event Source: The place where events occur is the individual components (buttons, Windows)->event: Events encapsulate specific things that occur on an interface component, if the program requires        Information about events that occur on a live interface component is generally obtained through the event object. ->event Listener: Responsible for monitoring the event source of events, and to make corresponding corresponding to the various events.
       android uses delegated (delegation) event handling: An ordinary component (event source) delegates the entire event handler to a specific object (event listener) when the event                       Source When a specified event occurs, the event handler (the instance method in the event listener) that notifies the delegate responds accordingly. The key to              event programming is to implement event listener classes. If there is more information to be passed to the event listener when the event occurs, the event information needs to be encapsulated into an event object,         It will be passed in as a parameter to a handler function.         Listener interface: Onclicklistener: Click events                   &NBSP ;          oncreatecontentmenulistener: Creating context Menu Events               and nbsp              onfocuschangelistener: Focus Change events             &NBS P                onkeylistener: Key events                              onlongclicklistener: Long Click events                              ontouchlistener: Touch screen Events
Implement the event listener form in the program: 1> the Inner class form: Defines the event listener as the inner class of the current class, and 2> the outer class form: When an event listener is shared by multiple GUI interfaces; 3>activity itself acts as an event listener class: Let ACTI Vity itself implements the listener interface and implements the event processing method; 4> internal Anonymous class: Creates an event listener object with an internal anonymous class because the event handler has no reuse value. 2. Callback-based event processing (improved in-process cohesion)Unlike the event listener mechanism, this event is handled by the event source itself after a specific event has occurred for the event source. Users in the GUIan event is fired on the component, and the componenttheir own specificmethod will be responsible for handling the event. We can only inherit the GUI class and override the event-handling method for that class. The so-called enhanced cohesion is the event-handling approachencapsulated internally, it is more suitable for dealing with that sort of event-handling logic to compare fixed components.
Callback-based event propagation: A callback-based event-handling method has a Boolean-type return value that identifies whether the processing method can handle the event fully and is true. Override the Ontouchevent method to display the corresponding touchscreen event.
2. Events for the corresponding system settings
The application listens for changes to the system settings, making changes to the system settings accordingly.     The configuration class is used to describe information on a mobile device (user-specific configuration items, system dynamic device configuration).     Gets the configuration object for the system: Configuration CFG = Getresources (). GetConfiguration (); Get System configuration Information Common properties: P210
Listen for changes to system settings: Override onconfigurationchanged to respond to system settings changes. Use activity's setrequestedorientation (int) To modify the orientation of the screen. 3.Handler message passing mechanism
    Why only UI processes are allowed to modify UI components in the activity?     If there are multiple threads concurrently manipulating the UI component, it can cause thread safety issues.     When a program starts for the first time, Android launches a main thread, main thread, which is primarily responsible for handling UI-related components.         Handler The meaning of existence?     Android only allows UI threads to modify the UI component of the activity, causing the newly-started thread to dynamically modify the property values of the interface components, which need to be implemented with handler's message delivery     mechanism.        Handler: sends a message to the MessageQueue managed by Looper and is responsible for handling messages Looper it.     Looper action: 1 looper per thread to manage MessageQueue, the message is constantly taken out of the MessageQueue and the message is assigned to the pair should be handler processed.                          looper There are 2 types of creation: 1. The main UI system itself initially The Looper object, so the program directly creates the handler to send processing messages through it.                          ,         &NB Sp                              2. To start a child thread yourself, you must create a Looper object, and Start, Looper.prepare () method                                                  ,         &NB Sp         and Looper.loop ().                             The thread here has to be a handmessage thread. Data from the main thread and other threads must be:                          &NBSP ;                          ,         &NB Sp                                LOOPER.PREP Is ()                                 &NBSP ;                          ,         &NB Sp                         &nbsp  handmessage (Message msg)                           and nbsp                          ,         &NB Sp                                  loop Er.loop     Cycle     MessageQueue role: Managed by Looper, FIFO mode
4. Asynchronous tasks (Asynctask)In order to solve the problem that the new thread cannot update the UI, Android has the following solution: 1.Handler to implement communication between threads 2.activity.runonuithread (Runnable) (Runnable) At the beginning of 4.view.postdelayed (Runnable,long), Asynctask is lighter and more suitable for simple asynchronous operations, without the need for threading and handler. P220

From for notes (Wiz)

Android Event handling

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: 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.