下面是获取数据:
Intent intent =getIntent();// 收取 email
Bundle bundle =intent.getBundleExtra("key");// 打开 email
bundle.getBoolean("boolean_key");// 读取内容
bundle.getString("string_key");
不过使用Bundle传递数据稍显麻烦,如果你只需要传递一种类型的值可以这样:
Intent intent =new Intent(EX06.this,OtherActivity.class);
intent.putExtra("boolean_key", true);
intent.putExtra("string_key", "string_value");
startActivity(intent);
获取数据:
Intent intent=getIntent();
intent.getBooleanExtra("boolean_key",false);
intent.getStringExtra("string_key");
2. 通过静态变量传递数据
适用于不可序列化的且简单的对象,不过不推荐使用静态代码块
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btOpenOtherActivity);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//定义一个意图
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
//改变OtherActivity的三个静态变量的值
OtherActivity.name = "wulianghuan";
OtherActivity.age = "22";
OtherActivity.address = "上海闵行";
startActivity(intent);
}
});
}
}
public class OtherActivity extends Activity {
public static String name;
public static String age;
public static String address;
private TextView text_name;
private TextView text_age;
private TextView text_address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
text_name = (TextView) findViewById(R.id.name);
text_age = (TextView) findViewById(R.id.age);
text_address = (TextView) findViewById(R.id.address);
//设置文本框的数据
text_name.setText("姓名:"+name);
text_age.setText("年龄:"+age);
text_address.setText("地址:"+address);
}
}










