public class CallBackTools { //接口 public interface OnListener { void execute(); } //声明接口 OnListener callBack; //被调用,执行接口实现 public void callListen() { callBack.execute(); } //外部进行接口实现 public void setCallBackListener(OnListener listener) { this.callBack = listener; } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final CallBackTools callBackTools = new CallBackTools(); callBackTools.setCallBackListener(new CallBackTools.OnListener() { @Override public void execute() { // do waht you want when this callback be called Log.e("callback","success"); } }); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { callBackTools.callListen(); } }); }
时间: 2024-08-24 23:13:43