Content
- Listener-based event model
- Callback-based event model
Android supports two event models: listener-based event models and callback-based event models.
Listener-based event model
The listener-based event model is a delegated, more "Object-Oriented" event processing, which is the same as Java (or C.
The event listening processing model has three objects:
- Event Source, that is, the GUI component.
- Event, that is, Event object, information about the Event on the GUI component.
- Event Listener monitors events in the Event source and responds to events accordingly.
Among them, the event source is the easiest to create, and any GUI component can be used as the event source. The event is generated automatically by the system without the programmer's concern; the event listener is the core of the entire event processing.
For example, when a button is clicked, the listener is triggered and changes to "button clicked" in the text box ".
public class EventQs extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Obtain the bn button in the Application
Button bn = (Button) findViewById(R.id.bn);
// Bind the event listener to the button.
bn.setOnClickListener(new MyClickListener());
}
// Define a listener for a click event
class MyClickListener implements View.OnClickListener
{
// Method required to implement the listener class. This method will be used as an event processor.
@Override
public void onClick(View v)
{
EditText txt = (EditText) findViewById(R.id.txt);
Txt. setText ("bn button clicked! ");
}
}
}
From this example, the steps of the event processing model are as follows:
- Event source, that is, the button whose ID is bn.
- The MyClickListener class is the event listener, which must be implemented by programmers.
- Register the listener. You only need to call the setOnClickListener method of the event source.
The event source can be any GUI component, without the involvement of too many programmers. Only one line of code is required to register the listener. Therefore, the key to event processing is the event listener MyClickListener.
In addition, the event listener MyClickListener exists as an internal class in this example. The external class and Activity itself can also be used as the event listener class and anonymous internal class.
Callback-based event model
If the event listening mechanism is a delegated event processing mechanism, the callback mechanism is the opposite: For the callback-based event processing model, the event source and the event listener are consistent, or the event listener disappears completely. When a user triggers an event on a GUI component, the component's own method is responsible for handling the event.
Almost all callback-based event processing methods have a boolean type return value, which is used to identify whether the processing method can fully process the event:
- If true is returned, it indicates that the processing method has completely handled the event and does not need to be propagated.
- If false is returned, it indicates that the processing method does not fully process the event and needs to be propagated.
For callback-based event propagation, events on a component not only trigger the callback method on the component, it will also trigger the callback method of the Activity where the component is located-as long as the event can be propagated to the Activity.
For example, you can customize the Button control and reconstruct its onKeyDown event.
public class MyButton extends Button
{
public MyButton(Context context, AttributeSet set)
{
super(context, set);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
Log.v("-crazyit.org-", "the onKeyDown in MyButton");
// Return true, indicating that the event will not spread outward
return true;
}
}
For Android listener-based event processing, the main method is to bind a specific event listener to the Android GUI component. For callback-based event processing, the main method is to override the specific callback method of the Android component.
In general, callback-based event processing can be used for some universal events, so the code is concise. When callback-based event processing cannot be used, listener-based event processing is used.
Download internal classes as Event Listeners
Download external Class As event listener
Download Activity as event listener
Download anonymous internal class as event listener
Bind download events to tags
Download the Custom Button control and override the onKeyDown callback event
Download callback-based event Propagation
Download and override the onTouchEvent callback event