您的位置:首页 > 其它

Activity之间的数据交换(简单的数据,类对象,图片)

2015-12-18 16:35 453 查看
简单数据的交换:

Bundle bundle=new Bundle();
bundle.putString("name", "star");
bundle.putInt("age", 23);
intent.putExtras(bundle);
startActivity(intent);


Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String name=bundle.getString("name");
int age=bundle.getInt("age");
类对象的数据交换:

Person person =new Person(21, "star", "Guangzhou");
Bundle bundle=new Bundle();
bundle.putSerializable("person", person);
intent.putExtras(bundle);
startActivity(intent);


Person类(注意要实现序列化的接口):

public class Person implements Serializable{
private int age;
private String name;
private String address;

public Person(int age,String name,String address){
this.age=age;this.name=name;
this.address=address;
}

public String toString(){
return "name="+name+",age="+age+",address"+address;
}
}
提取数据传递的类信息:

Person person =(Person) intent.getSerializableExtra("person");
textView.setText(person.toString());
传递图片:

Bundle bundle=new Bundle();
Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bundle.putParcelable("bitmap", bitmap);
intent.putExtras(bundle);
startActivity(intent);


Bitmap bitmap=intent.getParcelableExtra("bitmap");
imageView.setImageBitmap(bitmap);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: