Understanding the Android event handling mechanism

Source: Internet
Author: User

The event handling mechanism of Android is divided into two main categories: listener-based and callback-based

1. Listener-based event handling

This approach is almost identical to the Java GUI Component listener, which is a delegate-like approach. That is, if the View component ( event source ) is clicked or long-pressed ( event ), the system delegates the click event to a specific object ( listener ), which can be a listener object or an event-handling method specified by the component. There are three members involved: Event source, event, and listener. Where the listener is the core, it contains the implementation of the event's processing.

The following is a simple button listener, which takes the form of an anonymous inner class implementation. The interface layout file is just a button under the linear layout file, which is relatively simple and is not given.

  

1  Public classTestactivityextendsActivity {2 3     4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.main);8         //Get button Component9Button btn=(Button) Findviewbyid (R.ID.BTN);Ten         //set up listeners for this component OneBtn.setonclicklistener (NewOnclicklistener () { A              -             //overloaded Event handling methods - @Override the              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub -                 //a simple hint of text -Toast.maketext (testactivity. This, +"Button was clicked", - toast.length_short). Show (); +             } A              at         }); -     } -}

As you can see, the programming steps of the processing model are broadly divided into three steps:

First, get the component Findviewbyid, that is, the event source;

Second, write the Listener class, the class to implement a Xxxlistener interface;

Third, invoke the event source's Setxxxlistener method to register the listener with the component.

2. Callback-based event handling

To understand callback-based event handling, first understand what a callback is.

The callback (Callback, which is called when the call and back is performed by the main function), refers to a piece of executable code that passes through the function arguments to the other code. This design allows the underlying code to invoke subroutines defined at the top level. (From Wikipedia)

A function callback in C + + is implemented by pointers, that is, by passing in a function pointer, which is implemented in Java through an interface or an inner class .

For an understanding of the Java callback mechanism, you can refer to this very good article http://hellosure.iteye.com/blog/1130176

In the callback mechanism, if you define a component in the interface layout file, this time the system thinks you register a callback, when the component is clicked or long time, it will trigger the callback event, the system will invoke the corresponding callback method, such as Boolean onKeyDown (int keycode, KeyEvent event), of course you can also customize the callback method by overloading it.

Here is an example

1  Public classMyButtonextendsButton2 {3      PublicMyButton (Context context,attributeset set) {4         Super(context,set);5     }6     //Custom Callback Methods7 @Override8      Public BooleanOnKeyDown (intkeycode,keyevent Event) {9         Super. OnKeyDown (keycode,event);Ten         return true; One     } A}

In fact, listener-based event handling also uses a callback function.

3, the difference between the two ways

In the event propagation order is the listener-and callback method->activity, so you can know that the processing mechanism of Android is to ensure that the event listener is first triggered. At the same time, the method of event monitoring makes the separation of event source and event listener, has better maintainability, has obvious advantage in dealing with special business logic, and callback-based method is more suitable to handle those common and business logic fixed events because it unifies the event source and listener.

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.