程序员人生 网站导航

android 回调机制实例!

栏目:互联网时间:2014-11-26 08:49:00

具体实现为在类中定义接口,在接口的实现方法中传入参数(也能够不传)。

在调用类中传入新建的接口,并实现未实现的方法。

public class CallBackClass { //传入相应的接口作为参数 public void huidiao(final runDate rundate) { //使用线程代替系统的事件 new Thread() { int i = 0; @Override public void run() { super.run(); while(true) { i++; //传入回调参数 rundate.hui("第" + i + "次回调参数!"); try { sleep(5000); } catch(InterruptedException e) { e.printStackTrace(); } } } }.start(); } //定义接口 public interface runDate { public void hui(String str); } }
在主函数中传入接口参数:

public class test { public static void main(String [] args) { CallBackClass callback = new CallBackClass(); callback.huidiao(new CallBackClass.runDate() { @Override public void hui(String str) { System.out.println(str); } }); } }
下1篇自定义ListView中也会用到接口回调。


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐