一、intent传值: 分为三个方法:
一:intent.putExtra传值,
二:bundle传值 分两种 putExtra 和putExtras
三:User类传值,
发送的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
//i.putExtra("key", "嘟嘟嘟嘟的");
//i.putExtra("key1", "啦啦啦啦啦了");
//``Bundle bundle=new Bundle();
//bundle.putString("a", "y");
//bundle.putString("c", "z");
//``i.putExtras(bundle);
//```i.putExtra("abc", bundle);
User user=new User("啊啊啊", 18);
i.putExtra("key", user);
startActivity(i);
}
});
接受的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.tv);
Intent intent = getIntent();
//Bundle b=intent.getExtras();
//``Bundle b=intent.getBundleExtra("abc");
//``String s=b.getString("a");
//``String s1=b.getString("c");
//String s = intent.getStringExtra("key");
//String s1=intent.getStringExtra("key1");
User user=(User) intent.getSerializableExtra("key");
tv.setText(user.getName()+user.getAge());
}
本文介绍了在Android应用程序中使用Intent、Bundle以及自定义User类进行数据传递的方法。通过示例展示了如何从一个活动(Activity)向另一个活动发送数据,并在接收端如何获取这些数据。

3万+

被折叠的 条评论
为什么被折叠?



