自己写的回调方法示例,代码如下,自己看吧,不难。
package com.tomyzhou.callbackdemo.service;
public class MyView {
private DataoperatorListenerListener operatorListener;
private int result;
public MyView() {
}
public void setDataOperatorListener(DataoperatorListenerListener listener) {
this.operatorListener = listener;
}
public void addData(int num1, int num2) {
if (operatorListener != null) {
this.result = operatorListener.add(num1, num2);
operatorListener.after(new String(String.valueOf(result)));
}
}
public interface DataoperatorListenerListener {
public int add(int num1, int num2);
public void after(String result);
}
}
----------------------------------------------------------------------------------------------------------------------------------
package com.tomyzhou.callbackdemo;
import com.tomyzhou.callbackdemo.service.MyView;
import com.tomyzhou.callbackdemo.service.MyView.DataoperatorListenerListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CallBackDemoActivity extends Activity {
private Button bt_add;
private TextView tv_result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_add = (Button) findViewById(R.id.button1);
tv_result = (TextView) findViewById(R.id.tv_result);
}
public void add(View view) {
MyView mView = new MyView();
mView.setDataOperatorListener(new DataoperatorListenerListener() {
@Override
public int add(int num1, int num2) {
System.out.println("正在运算中...");
return num1 + num2;
}
@Override
public void after(String result) {
tv_result.setText(result);
}
});
mView.addData(10, 20);
}
}