Android learns key event monitoring and command mode
1. Command mode
Intent:
Encapsulate a request as an object so that you can parameterize the customer with different requests;
Queue or record request logs for requests and support unrecoverable operations.
The request is encapsulated into an object. When a request is submitted to an object, we do not need to know the operation of the specific request or the recipient of the request,
The coupling between the requester object of an action and the executor object of the action is realized.
Applicability:
- Use command mode to replace callback applications;
- Specify, arrange, and execute requests at different times to dynamically change requests;
- Cancellation is supported;
- Logs can be modified to save the request execution process for recovery;
- The command mode supports calling a group of transactions. It is easy to add and expand new transactions;
(What is transaction reference: http://book.51cto.com/art/201202/319377.htm)
Structure:
Note:
Command: it is the encapsulation of a command and declares the operation interface for executing the command;
Concretecommand: enables the specific receiver of the command to execute the corresponding action;
Invoke: sends a request to execute the command, and stores the abstract parent class of the specific command executor;
Aggreger: Specifies the creator of the command object to perform specific operations on specific requests;
This figure shows the core of this mode: Call invoke of the operation object is decoupled from the worker of the object that implements the operation,
Is to add a command to delegate the action execution to the command to execute the call to the real action performer.
2. onclicklistener in Android
In the View class:
Public interface onclicklistener {
Void onclick (view V );
}
Purpose: interface definition for a callback to be invoked when a view is clicked.
Is the callback interface of a click event.
In the view that needs to respond to the click event, you must register a corresponding clicklistener to listen for the call when the click event occurs.
Let's take a look at this section.Code: Button control
Public Class Myactivity Extends Activity { Protected Void Oncreate (bundle icicle ){ Super . Oncreate (icicle); setcontentview (R. layout. content_layout_id ); Final Button button = (Button) findviewbyid (R. Id. button_id ); // Register the Click Event listener Button. setonclicklistener ( New View. btnonclicklistener ());} Private Class Btnonclicklistener Extends Onclicklistener { Public Void Onclick (view v ){ // Perform action on click }}}
Take a look at the corresponding structure:
View is invoke, onclicklistener is command, and myactivity is worker er.
The execution process is as follows:
You can see that the application in this method belongs:
Replace the callback model in the form of the callback function in command mode to make the framework structure clearer and more flexible.