Android Learning key event monitoring and command mode

Source: Internet
Author: User

Android Learning key event Monitoring and command mode-dufresne-Blog Park

Android Learning key event monitoring and command mode

?

one Command mode

Intention:

Encapsulates a request as an object so that you can parameterize the customer with different requests;

Queue or log request logs for requests, and support for actions that can be undone.

    The request is encapsulated as an object, and when a request is submitted to an object, it allows us to not have to know the specific requested operation or the recipient of the request.

It implements the decoupling between the requestor object of the action and the performer object of the action.

Applicability:

    1. Use the command mode instead of the callback application in callback form;
    2. Specify, arrange, and execute requests at different times, dynamic change requests;
    3. Support Cancel operation;
    4. Support to modify the log, save the request execution process to restore operations;
    5. The command mode supports invocation operations for a set of transactions. and easy to increase the expansion of new transactions;?

(What is a transaction reference: http://book.51cto.com/art/201202/319377.htm)

Structure:

??????

    

Description

Command: The wrapper of the order, which declares the operation interface of the command execution;

Concretecommand: The specific receiver of the command to perform the corresponding action;

Invoke: Issuing a request to execute the command, storing the abstract parent class of the specific command performer;

Receiver: The implementation and execution of specific related requests, specified by the creator of the Command object;

?

  This diagram allows you to get to the core of this pattern: the implementation of invoke invoke of the action object is decoupled from the receiver that specifically implements the operation,

is to execute a call to the real action performer by adding the command to the command to perform the execution of the action.

?

two android in Onclicklistener

In the View class:

Public interface Onclicklistener {

void OnClick (View v);

}

function: Interface definition for a callback to being invoked when a view is clicked.

is a callback interface for the Click event.

In the view that needs to respond to the Click event, you need to register a corresponding clicklistener to listen for the Click event that is called when it occurs.

Take a look at the following code: Button control

  

 Public classMyActivityextendsActivity {protected voidonCreate (Bundle icicle) {Super. OnCreate (Icicle);         Setcontentview (r.layout.content_layout_id); FinalButton Button =(Button) Findviewbyid (r.id.button_id); //Register the Click event ListenerButton.setonclicklistener (NewView.btnonclicklistener ()); }Private classBtnonclicklistenerextendsonclicklistener{ Public voidOnClick (View v) {//Perform Action on Click       }}}

?

Take a look at the corresponding structure: ?

?

    

The view is Invoke,onclicklistener or command,myactivity is receiver.

The execution process is as follows:

  

    

?

You can see that the application of this method belongs to:

The command mode replaces the callback model in the form of the callback function, making the frame structure clearer and more flexible.

Android Learning key event monitoring and command mode

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.