Android Simple App (iii)--add listeners to your program

Source: Internet
Author: User

usually in writing programs often encounter listeners, such as button click Listener, Key Listener and so on. The Listener in Android and the callback function in Java are the same concept, which is to define an interface in the underlying code to invoke the high-level code. So what is a callback function? On the Internet, " in Windows, programmers want the system DLL to call a method of their own writing, and then use the DLL's callback function (CALLBACK) interface to write the program, make it call, this is called callback." When you invoke an interface, you need to strictly follow the defined parameters and method calls, and you need to handle the async of the function, which will cause the program to crash. This explanation seems to be more difficult to understand, here is a simple example, programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. "As I understand it, I dug a hole in program A and waited for you to fill in the content in program B. Not filled? That's the pit .

First, define the callback function

  1. Create a class that includes a variable for an interface
    Private CallBack CallBack; // the variables of the interface
  2. Create a declaration of an interface
     Public Interface callback{   publicvoid out ();   the method to be implemented inside the interface }
  3. Binding to an interface in a class (or getting a reference to an interface)
     Public void Setcallback (CallBack CallBack) {        this. CallBack = CallBack;    }           Public void out () {        callback.out ();        System.out.println ("This is the output of the Callerclass");    }
  4. the entire class
     Public classCallerclass {PrivateCallBack CallBack;//declaration of the interface         PublicCallerclass () {};  Public voidSetcallback (CallBack CallBack) { This. CallBack =CallBack; }         Public voidout () {callback.out (); System.out.println ("This is the output of Callerclass."); }         Public Interfacecallback{ Public voidOut ();//the method to be implemented in the interface    }    }
    View Code

Second, call the callback function

I am here to bind the interface in activity, the actual code is as follows:

 Public class extends Implements callerclass.callback{  private Callerclass Caller ...}

Inherited the callback interface in Callerclass, the Out method is as follows:

    @Override    publicvoid  out () {        //  TODO auto-generated Method Stub        System.out.println ("This is the output on the activity");    }

Finally, call the Out () method of Callerclass in OnCreate:

New Callerclass (); Caller.setcallback (this); caller. Out ();

Full code:

 Public classMainactivityextendsActivityImplementscallerclass.callback{PrivateCallerclass Caller; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Caller=NewCallerclass (); Caller.setcallback ( This); Caller.            Out (); } @Override Public voidout () {//TODO auto-generated Method StubSystem.out.println ("This is the output on the activity"); }}

Iii. Results

perfect~

The sense of interface is similar to the pointer in C + +, which is a reference to an address but does not generate memory. So we can give the program a way to execute it beforehand. More than that, interface in the design of the role of the model is more powerful, Factory mode, builder mode, the Observer mode is used to interface.

Android Simple App (iii)--add listeners to your program

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.