Android callback mechanism instance!

Source: Internet
Author: User

The concrete implementation is to define the interface in the class, and pass in the parameters in the implementation method of the interface (also can not pass).

In the calling class, pass in the newly created interface and implement the method that is not implemented.

public class Callbackclass {//pass in the appropriate interface as parameter public void Huidiao (final rundate rundate) {//Use thread instead of system event new thread () {int i = 0;@ overridepublic void Run () {super.run (); while (true) {i++;//incoming callback parameter Rundate.hui ("+ i +  " times callback parameter!) "); Try{sleep (5000);} catch (Interruptedexception e) {e.printstacktrace ();}}}}. Start ();} Define interface public interface rundate{public void Hui (String str);}}
To pass in the interface parameters in the main function:

public class Test {public static void main (String [] args) {Callbackclass callback = new Callbackclass (); Callback.huidiao (n EW callbackclass.rundate () {@Overridepublic void Hui (String str) {System.out.println (str);}});}}
An interface callback is also used in the next custom ListView.


Android callback mechanism instance!

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.