最近在做一个项目,里面很多地方都用到了函数回调机制
现在做一个demo说明一下
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ResultActivity extends Activity implements OnClickListener{
private Button btn1;
private Button btn2;
private EditText edt1;
private EditText edt2;
private int REQUEST_TO_ACTIVITY_A = 0;
private int REQUEST_TO_ACTIVITY_B = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
edt1 = (EditText) findViewById(R.id.editText1);
edt2 = (EditText) findViewById(R.id.editText2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
//这里函数命名是固定的 onActivityResult 而我刚开始命名为onActivityForResult导致一直收不到数据
//三个参数的意义
//requestCode 这个是请求回调指令,该指令可以自己命名,用于判断是哪个请求
//resultCode 这个是结果回复指令
//data 里面存放回调的数据 在接收回调数据时 应用 Bundle extra = data.getExtras(); 这里容易出现错误
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_TO_ACTIVITY_A){
switch(resultCode){
case RESULT_OK:
Bundle extra = data.getExtras();
edt1.setText(extra.getString("name"));
break;
}
}else if(requestCode == REQUEST_TO_ACTIVITY_B){
switch(resultCode){
case RESULT_OK:
Bundle extra = data.getExtras();
edt2.setText(extra.getString("name"));
break;
}
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn1)){
Intent intent = new Intent(ResultActivity.this, Activity_A.class);
startActivityForResult(intent, REQUEST_TO_ACTIVITY_A);
}else if(arg0.equals(btn2)){
Intent intent = new Intent(ResultActivity.this, Activity_B.class);
startActivityForResult(intent, REQUEST_TO_ACTIVITY_B);
}
}
}
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity_A extends Activity implements android.view.View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_a);
btn = (Button) findViewById(R.id.button1_a);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn)){
Intent intent = new Intent();
intent.putExtra("name", "从Activity_A得到的回调数据");
setResult(RESULT_OK, intent);
finish();
}
}
}
package com.example.testresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity_B extends Activity implements android.view.View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_b);
btn = (Button) findViewById(R.id.button1_b);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.equals(btn)){
Intent intent = new Intent();
intent.putExtra("name", "从Activity_B得到的回调数据");
setResult(RESULT_OK, intent);
finish();
}
}
}
//Activity_A、Activity_B的xml布局文件里只有一个Button按钮 没有其他的布局了
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。