假设有两个Activity,主界面A,功能界面B,由A启动B,并传数据给B,B在经过处理后把数据传回给A。
先是A传B:
Bundle bundle = new Bundle();
bundle.putString("Dir", "/sdcard");
Intent intent=new Intent();
intent.putExtras(bundle);
intent.setClass(A.this,B.class);
A.this.startActivityForResult(intent,0);
//这里的0代表requestCode,就是用来做个标记(要求是大于等于0的整数);
然后就是B接收再传回:
Intent it = new Intent();
Bundle bundle=it.getExtras();
String mString=bundle.getString("Dir");
mString=mString+"/"
bundle.putString("Dir",mString);
B.this.setResult(0, it);//0与前面A里的0对应
finish();
A最后再接收B回传的结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//B返回时触发
}
最后以一个SDK开发大全上面的例子来加深理解
先是A传入B,并且把接收B传回结果的接收器写在A中
package com.my;
/* import相关class */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class A extends Activity
{
private EditText et;
private RadioButton rb1;
private RadioButton rb2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/*取得输入的身高*/
et = (EditText) findViewById(R.id.height);
double height=Double.parseDouble(et.getText().toString());
/*取得选择的性别*/
String sex="";
rb1 = (RadioButton) findViewById(R.id.sex1);
rb2 = (RadioButton) findViewById(R.id.sex2);
if(rb1.isChecked())
{
sex="M";
}
else
{
sex="F";
}
/*new一个Intent对象,并指定class*/
Intent intent = new Intent();
intent.setClass(A.this,B.class);
/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用Activity B*/
startActivityForResult(intent,0);
}
});
}
/* 覆盖 onActivityResult()*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
switch (resultCode)
{
case 0:
/* 取得来自Activity2的数据,并显示于画面上 */
Bundle bunde = data.getExtras();
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
et.setText(""+height);
if(sex.equals("M"))
{
rb1.setChecked(true);
}
else
{
rb2.setChecked(true);
}
break;
default:
break;
}
}
}
然后是B接收到A,再回传给A
package com.my;
/* import相关class */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class B extends Activity
{
Bundle bunde;
Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 载入mylayout.xml Layout */
setContentView(R.layout.myalyout);
/* 取得Intent中的Bundle对象 */
intent=this.getIntent();
bunde = intent.getExtras();
/* 取得Bundle对象中的数据 */
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
/* 判断性别 */
String sexText="";
if(sex.equals("M"))
{
sexText="男性";
}
else
{
sexText="女性";
}
/* 取得标准体重 */
String weight=this.getWeight(sex, height);
/* 设置输出文字 */
TextView tv1=(TextView) findViewById(R.id.text1);
tv1.setText("你是一位"+sexText+"/n你的身高是"+height+
"厘米/n你的标准体重是"+weight+"公斤");
/* 以findViewById()取得Button对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 返回result回上一个activity */
B.this.setResult(0, intent);
/* 结束这个activity */
B.this.finish();
}
});
}
/* 四舍五入的method */
private String format(double num)
{
NumberFormat formatter = new DecimalFormat("0.00");
String s=formatter.format(num);
return s;
}
/* 以findViewById()取得Button对象,并添加onClickListener */
private String getWeight(String sex,double height)
{
String weight="";
if(sex.equals("M"))
{
weight=format((height-80)*0.7);
}
else
{
weight=format((height-70)*0.6);
}
return weight;
}
}
转载 http://blog.csdn.net/boyupeng/article/details/6336496