Android event processing model

Source: Internet
Author: User

Android event processing model

Event processing is a very important part of Android programming, because Android applications generate many kinds of events (Actions) during running ), the application must execute the corresponding response code for these actions. The method that associates the generated actions with the response is the event processing mechanism. The Android platform provides two event processing mechanisms: listener-based mode and callback-based mode.

1. listener-based event processing model Part 1
Event Source: Where an Event occurs, such as a button or text box Event: Event type. The Event listener is expressed through the Event object. It is responsible for listening to an Event type and performing corresponding operations.

In the listener-based event processing model, you must first create an event listener that can listen to a certain type of events. Then, register the event listener to a component; when an action can be monitored by the listener occurs on the component, the component will generate an Event object representing the action and pass the Event object to the listener. In Android, generating the Event object and passing the Event object do not require the programmer's intervention. The system will automatically process this step. What programmers need to do is to specify the event source, register the event listener corresponding to an event type on the event source, and rewrite the corresponding processing code.

Xml layout code


          
     

Java code

Public class MainActivity extends Activity {private Button mClickBtn; // create a listener and listen to the "click" event final View. onClickListener listener = new View. onClickListener () {@ Override public void onClick (View v) {L. d ("You click btn_click! ") ;};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mClickBtn = (Button) findViewById (R. id. btn_click); // set the event type to 'click' for the mClickBtn component. setOnClickListener (listener );}

}

The above Code creates a listener in MainActivity, which listens to the "click" event and registers the listener to mClickBtn through the setOnClickListener method in the onCreate method.

In the above Code, the listener is implemented through an anonymous internal class, creates an object for the anonymous internal class, and registers the object to the event source, this method is better than creating a listener directly at the position of the listener (as shown below) because the listener object can be reused.

mClickBtn.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        L.d("You click btn_click!");    }});

Note:

Generally, listeners are implemented through internal classes because internal classes can access components or fields contained in external classes. Generally, listeners involve operations on related components or fields in external classes. If you use an external class, you also need to pass the reference of this class, and the code is not very intuitive. Generally, the listener does not implement the business logic. If the listener does need to process the logic, it can encapsulate the logic in external class methods, then call this method in the listener (the listener is only responsible for simple data presentation or data transmission, and logical processing is implemented through a special method. This encoding method is more in line with the object-oriented design principles)
2. Callback-based event processing model

The listener-based processing method mentioned above is a delegated event processing method, that is, entrusting event processing to a listener responsible for event processing; the callback-based processing method is an active event processing method, that is, the event is handled by the event source. In terms of code implementation, components to add event processing must implement specific interfaces and rewrite some methods in Interfaces. When a corresponding event occurs, the system calls back the override method in this component. For example

XML layout code


      
   
  

Java code

// Custom MyButton class public class MyButton extends Button {public MyButton (Context context, AttributeSet set) {super (context, set );} // set the callback function @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. I ("MyButton", "MyButton onKeyDown"); return false ;}} public class MainActivity extends Activity {private Button mClickBtn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mClickBtn = (Button) findViewById (R. id. btn_click); // sets the listener mClickBtn. setOnKeyListener (new View. onKeyListener () {@ Override public boolean onKey (View v, int keyCode, KeyEvent event) {Log. I ("mClickBtn", "onKeyListener"); return false ;}});} // sets the Activity callback function @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. I ("MainActivity", "MainActivity onKeyDown"); return false ;}}

After running the above code, it will print in logcat:
OnKeyListener
MyButton onKeyDown
MainActivity onKeyDown

That is to say, the sequence of calls after an event is listener> callback function of the component. When the value returned by the callback function is false, the event continues to spread upwards, therefore, the callback function in Acitivity will continue to be called. If the onKey method in the listener returns true, the callback function of the component will not be called.

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.