在一个activity中启动另一个activity并等待传输数据,即startActivityForResult()的使用

来源:互联网 发布:usb口禁用软件 编辑:程序博客网 时间:2024/06/05 18:15

第一个activity -------------Main


package com.webabcd.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Main extends Activity {

TextView txt;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);


txt = (TextView) this.findViewById(R.id.txt);
txt.setText("Activity 1");


Button btn = (Button) this.findViewById(R.id.btn);
btn.setText("启动另一个Activity");
btn.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

// 实例化 Intent,指定需要启动的 Activity
Intent intent = new Intent();
intent.setClass(Main.this, MyActivity.class);


// 实例化 Bundle,设置需要传递的参数
Bundle bundle = new Bundle();
bundle.putString("name", "webabcd");
bundle.putDouble("salary", 100.13);


// 将需要传递的参数赋值给 Intent 对象
intent.putExtras(bundle);


// startActivity(intent); // 启动指定的 Intent(不等待返回结果)
// Main.this.finish();

// 启动指定的 Intent,并等待返回结果
// 其中第二个参数如果大于等于零,则返回结果时会回调 onActivityResult() 方法
startActivityForResult(intent, 0);

}
});

Log.d("MyDebug", "onCreate");
}

// 被启动的 Activity 返回结果时的回调函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
       Bundle bundle = data.getExtras();
       
       String name = bundle.getString("name");
       double salary = bundle.getDouble("salary");
       
       txt.setText("Activity 1" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));
}
}


@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();

Log.d("MyDebug", "onStart");
}


@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

Log.d("MyDebug", "onStop");
}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();

Log.d("MyDebug", "onRestart");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

Log.d("MyDebug", "onPause");
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

Log.d("MyDebug", "onResume");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

Log.d("MyDebug", "onDestroy");
}

}


第二个activity ------------- MyActivity

package com.webabcd.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


// 被另一个 Activity 所启动的 Activity
public class MyActivity extends Activity {

Intent intent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main2);


// 获取启动者传递过来的参数
intent = this.getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
double salary = bundle.getDouble("salary");

TextView txt = (TextView) this.findViewById(R.id.txt);
txt.setText("Activity 2" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));


Button btn = (Button) this.findViewById(R.id.btn);
btn.setText("返回前一个Activity");
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 返回参数给启动者
MyActivity.this.setResult(Activity.RESULT_OK, intent);
MyActivity.this.finish();
}
});
}
}


布局文件-------main1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/txt" />

<Button android:id="@+id/btn" android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</LinearLayout>


布局文件----mian2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/txt" />

<Button android:id="@+id/btn" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


原创粉丝点击