Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Adnroid回调方法示例

$
0
0

自己写的回调方法示例,代码如下,自己看吧,不难。

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);
}
}


作者:nnmmbb 发表于2013-1-28 22:38:26 原文链接
阅读:64 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>