码迷,mamicode.com
首页 > 其他好文 > 详细

Intent 传递对象的方法方式

时间:2017-12-07 23:43:08      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:zab   set   简单   为我   span   方法   start   his   color   

 1 一、Serializable 方式
 2 这是最简单的一种方法,因为我们要做的就是让我们自定义的对象实现 Serializable 这个空接口。
 3 public class Person implements Serializable{
 4     private String mName;
 5     private String mAddress;
 6 
 7     public String getName() {
 8       return mName;
 9     }
10 
11     public void setName(String name) {
12         mName = name;
13     }
14 
15     public String getAddress() {
16         return mAddress;
17     }
18 
19     public void setAddress(String address) {
20         mAddress = address;
21     }
22 }
23 这个时候,就已经可以使用Intent 的putExtra() 方法传递这个自定义对象了,在Activity中这样使用
24       Person person = new Person();
25       person.setName("Hwaphon");
26       person.setAddress("Anhui");
27 
28       Intent intent = new Intent(MainActivity.this,SecondActivity.class);
29       intent.putExtra("person",person);
30       startActivity(intent);
31       finish();
32 在Activity中接收数据
33 Intent intent = getIntent();
34 Person person = (Person) intent.getSerializableExtra("person");

 

Intent 传递对象的方法方式

标签:zab   set   简单   为我   span   方法   start   his   color   

原文地址:http://www.cnblogs.com/dame/p/8001476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!