Content of this article
- An event model based on listening
- Callback-based event model
Android supports two event models, a listener-based event model, and a callback-based event model.
An event model based on listening
The listener-based event model is a commissioned, more "object-oriented" event handler, which is the same as in Java (or C #).
The event listener processing model has three objects:
- Event source, which is the GUI component.
- Event, the event object, and information about events that occur on the GUI component.
- Event Listener, which listens to events that occur at the source of the event and responds accordingly to various events.
Where the event source is easiest to create, any GUI component can act as the source of the event, and the event is generated automatically by the system without the programmer's concern, and the event listener is the core of the whole event processing.
For example, when the button is clicked, the listener is triggered and becomes "button clicked" within the text box.
Public class EVENTQS extends Activity
{
@Override
Public void onCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get the BN button in the application
Button bn = (Button) Findviewbyid (R.ID.BN);
Binds the event listener to the button.
Bn.setonclicklistener (new
}
Define a listener for a click event
class Myclicklistener Implements View.onclicklistener
{
Implement the method that the listener class must implement, which will act as the event handler
@Override
Public void OnClick (View v)
{
EditText txt = (EditText) Findviewbyid (r.id.txt);
Txt.settext ("bn button was clicked! ");
}
}
}
From this example, the steps of the event-handling model are:
- The event source is the ID of the bn button.
- Event listeners, the Myclicklistener class is the event listener, which must be implemented by the programmer.
- Register the Listener, as long as you invoke the Setonclicklistener method of the event source.
Of these three things, the event source can be any GUI component, does not require too many programmers to participate, registers the monitoring also as long as one line of code, therefore, the event processing key is the event listener Myclicklistener.
In addition, the event listener Myclicklistener This example in the form of an internal class, as well as an external class, Activity itself as an event listener class, and an anonymous inner class.
Callback-based event model
If the event listener mechanism is a delegate-type event handler, the callback mechanism is the opposite: for callback-based event-handling models, the event source is unified with the event listener, or the event listener is completely gone. When a user fires an event on a GUI component, the component's own specific method is responsible for handling the event.
Almost all callback-based event-handling methods have a Boolean-type return value that identifies whether the processing method can handle the event fully:
- If true, indicates that the processing method has fully handled the event and does not need to propagate
- If False is returned, the processing method does not fully handle the event and needs to propagate
For callback-based event propagation, events that occur on a component not only trigger a callback method on that component, but also trigger a callback method for the activity that the component is in-as long as the event can propagate to that activity.
For example, a custom Button control that reconstructs its OnKeyDown event.
Public class MyButton extends Button
{
Public MyButton (context context, AttributeSet set)
{
Super (context, set);
}
@Override
Public boolean onKeyDown (intevent)
{
event);
LOG.V ("-crazyit.org-""the OnKeyDown in MyButton");
Returns true, indicating that the event does not spread outwards
return true;
}
}
The primary method for Android listener-based event processing is to bind specific event listeners for the Android GUI component, whereas for callback-based event handling, the main approach is to rewrite the Android component-specific callback method.
How to choose, in general, callback-based event processing can be used for some commonality of events, so that the code is concise, and can not use callback-based event processing, and then use listener-based event processing.
Download inner class as event listener
Downloading an external class as an event listener
Download activity itself as an event listener
Download anonymous inner class as event listener
Download event Bindings to tags
To download a custom Button control override OnKeyDown Callback event
Download callback-based event propagation
Download Rewrite Ontouchevent callback event