您的位置:首页 > 其它

不同Activity之间传递数据--Bundle对象和startActivityForResult方法的实现

2013-02-01 18:03 801 查看
首先,由于Activity是Android四大组件之一,如果一个应用程序中包含不止一个Activity,则需要在AndroidManifest.xml文件中进行声明。

例如进行如下的声明(程序中包含两个Activity):

<activity android:name=".EX03_10"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- 另一个Activity的声明 -->
</activity>
<activity android:name="EX03_10_1"></activity>


下面以两个例子说明不同的Activity之间传递数据的方法。

1.使用Bundle对象传递数据

传递数据使用的方法如下:

/*new一个Intent对象,并指定class*/
final Intent intent = new Intent();
intent.setClass(EX03_10.this,EX03_10_1.class);

/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用Activity EX03_10_1*/

startActivity(intent);


接收数据使用的方法如下:

/* 取得Intent中的Bundle对象 */
Bundle bunde = this.getIntent().getExtras();

/* 取得Bundle对象中的数据 */
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");


下面给出具体的实现代码:

1.1 主程序代码(第一个Activity)

public class EX03_10 extends Activity
{
/** 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)
{
/*取得输入的身高*/
EditText et = (EditText) findViewById(R.id.height);
double height=Double.parseDouble(et.getText().toString());
/*取得选择的性别*/ String sex="";
RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);
if(rb1.isChecked()) {
sex="M";
}
else{
sex="F";
}
/*new一个Intent对象,并指定class*/
final Intent intent = new Intent();
intent.setClass(EX03_10.this,EX03_10_1.class);

/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用Activity EX03_10_1*/

startActivity(intent);
}
});
}
}


1.2 第二个Activity的实现代码:

public class EX03_10_1 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.myalyout);

/* 取得Intent中的Bundle对象 */
Bundle bunde = this.getIntent().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+"公斤");
}

/* 四舍五入的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;
}
}


2.返回数据到前一个Activity

这里需要使用startActivityForResult方法

传递数据的方法与上一个例子是类似的,实现的代码如下:

/*new一个Intent对象,并指定class*/
Intent intent = new Intent();
intent.setClass(EX03_11.this,EX03_11_1.class);
/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用Activity EX03_11_1*/
startActivityForResult(intent,0);


注意:这里需要实现一个重载的方法:

protected void onActivityResult (int requestCode, int resultCode, Intent data)


在另一个Activity中获取数据的代码与上一个例子是相同的,实现如下:

/* 取得Intent中的Bundle对象 */
intent=this.getIntent();
bunde = intent.getExtras();
/* 取得Bundle对象中的数据 */
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");


下面给出这个例子的具体实现代码:

2.1 主程序的实现:

public class EX03_11 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(EX03_11.this,EX03_11_1.class);
/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用Activity EX03_11_1*/
startActivityForResult(intent,0);
}
});
}
/* 重写 onActivityResult()*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_OK:
/* 取得数据,并显示于画面上 */
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;
}
}
}


2.2 另一个Activity的实现:

public class EX03_11_1 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 */
EX03_11_1.this.setResult(RESULT_OK, intent);
/* 关闭activity */ EX03_11_1.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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐