Android:回调机制
public class Fruits { public interface Apple { public String info(); } } //匿名内部类 public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Fruits.Apple apple = new Fruits.Apple() { @Override public String info() { // TODO Auto-generated method stub return "I am an Apple"; } }; System.out.println(apple.info());// I am an Apple } } //继承接口 public class SonFruits implements Fruits.Apple { public static void main(String[] args) { // TODO Auto-generated method stub SonFruits sonFruits = new SonFruits(); sonFruits.info(); System.out.println("sonFruits.info()->" + sonFruits.info()); } @Override public String info() { // TODO Auto-generated method stub return "I am an Apple!"; } }
//回调方法的接口 public interface CallBack { public void callBackMethod(); } //回调方法的实现类 public class CallBackListener implements CallBack { @Override public void callBackMethod() { // TODO Auto-generated method stub System.out.println("回调"); } } //调用类 public class Caller { private CallBackListener callBackListener; public void call() { this.callBackListener.callBackMethod(); } public void setCalBack(CallBackListener callBackListener2) { this.callBackListener = callBackListener; } } //业务处理类 public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Caller caller = new Caller(); caller.setCalBack(new CallBackListener()); caller.call(); } }
Button点击事件
CallBack------->OnClickListener接口
CallBackListener--------->实现OnClickListener接口的类
Caller-------->Button对象
Main-------->业务处理类(Activity)
举个栗子
frameworks/base/core/java/android/view/GestureDetector.java public class GestureDetector { 1. public interface OnGestureListener {//内部 手势接口 2. public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener {//实现接口的类 will@s2:~/ZK-Rxxx_Android7.1/ZK_RXXX_RK3288_ANDROID7.1$ grep -rn "SimpleOnGestureListener" ./frameworks/base/services/ ./frameworks/base/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java:267: private final class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener { ./frameworks/base/services/core/java/com/android/server/display/OverlayDisplayWindow.java:363: new GestureDetector.SimpleOnGestureListener() { ./frameworks/base/services/core/java/com/android/server/wm/TaskTapPointerEventListener.java:181: private final class TwoFingerScrollListener extends GestureDetector.SimpleOnGestureListener { ./frameworks/base/services/accessibility/java/com/android/server/accessibility/AccessibilityGestureDetector.java:43:class AccessibilityGestureDetector extends GestureDetector.SimpleOnGestureListener { ./frameworks/base/services/accessibility/java/com/android/server/accessibility/MagnificationGestureHandler.java:26:import android.view.GestureDetector.SimpleOnGestureListener; ./frameworks/base/services/accessibility/java/com/android/server/accessibility/MagnificationGestureHandler.java:332: private final class MagnifiedContentInteractionStateHandler extends SimpleOnGestureListener will@s2:~/ZK-Rxxx_Android7.1/ZK_RXXX_RK3288_ANDROID7.1$ 3. /frameworks/base/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java private final class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener { 4. frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java // monitor for system gestures mSystemGestures = new SystemGesturesPointerEventListener(context, new SystemGesturesPointerEventListener.Callbacks() { //业务处理