关于activity之间回传的数据的问题,在网上搜索了很多,基本都是一样的,但是测试了结果却没有达到效果,现在记录下自己测试通过的代码:
Activity01.java
package com.yarin.android.Examples_03_01;
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 Activity01 extends Activity { private TextView textView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 设置显示main.xml布局 */ setContentView(R.layout.main); Log.v("qq", "22222222222222"); /* findViewById(R.id.button1)取得布局main.xml中的button1 */ Button button = (Button) findViewById(R.id.button1); textView = (TextView)findViewById(R.id.text1); textView.setText("没传值"); /* 监听button的事件信息 */ button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 新建一个Intent对象 */ Intent intent = new Intent(); /* 指定intent要启动的类 */ intent.setClass(Activity01.this, Activity02.class); /* 启动一个新的Activity */ Bundle bundle = new Bundle(); bundle.putString("name", "activity1传过来"); intent.putExtras(bundle); startActivityForResult(intent, 1333); /* 关闭当前的Activity */ //Activity01.this.finish();
} }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.v("aa", "3333333333333333"); String msg = data.getExtras().getString("msg"); Log.v("msg", msg+"111111111"); this.textView.setText(msg); // ...
} }
|
Activity02.java
package com.yarin.android.Examples_03_01;
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 Activity02 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 设置显示main2.xml布局 */ setContentView(R.layout.main2); /* findViewById(R.id.button2)取得布局main.xml中的button2 */ Button button = (Button) findViewById(R.id.button2); TextView textView = (TextView)findViewById(R.id.text2); Bundle bundle = getIntent().getExtras(); String name = bundle.getString("name"); textView.setText(name); /* 监听button的事件信息 */ button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 新建一个Intent对象 */ Intent intent = new Intent(); /* 指定intent要启动的类 */ intent.setClass(Activity02.this, Activity01.class); intent.putExtra("msg", "activity 2 传过来"); setResult(RESULT_OK, intent); finish(); /* 启动一个新的Activity */ //startActivity(intent);
/* 关闭当前的Activity */ //Activity02.this.finish();
} }); } }
|
阅读(1231) | 评论(1) | 转发(0) |