intent传递各种参数用法

上传人:第*** 文档编号:31774962 上传时间:2018-02-09 格式:DOCX 页数:7 大小:30.11KB
返回 下载 相关 举报
intent传递各种参数用法_第1页
第1页 / 共7页
intent传递各种参数用法_第2页
第2页 / 共7页
intent传递各种参数用法_第3页
第3页 / 共7页
intent传递各种参数用法_第4页
第4页 / 共7页
intent传递各种参数用法_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《intent传递各种参数用法》由会员分享,可在线阅读,更多相关《intent传递各种参数用法(7页珍藏版)》请在金锄头文库上搜索。

1、一、传递 List传递 List的方法java view plain copy1. ArrayList info = new ArrayList(); 2. 3. info.add(name); 4. 5. info.add(website); 6. 7. info.add(weibo); 8. 9. Intent intent = new Intent(MainActivity.this, ResultActivity.class); 10. 11. intent.putStringArrayListExtra(infoList, info); 12. 13. startActivity(i

2、ntent); 接收 List的方法java view plain copy1. ArrayList infoList = new ArrayList(); 2. 3. infoList = getIntent().getStringArrayListExtra(infoList); 传递 List的方法java view plain copy1. intent.putIntegerArrayListExtra(key, list); 接收 List的方法java view plain copy1. list =(ArrayList) getIntent().getIntegerArrayLi

3、stExtra(key); 二、传递 Object有两种方式来传递 Object:Serializable 和 Parcelable2.1 使用 Serializable 方式前提:Object 需要实现 Serializable 接口用 Serializable 方式传递 Object 的语法:bundle.putSerializable(key,object);用 Serializable 方式接收 Object 的语法:object=(Object) getIntent().getSerializableExtra(key);实现 Serializable 接口就是把对象序列化,然后再传

4、输,和 Java 的常用编程没什么明显区别,而且 Object 不需要明显改变,推荐用这种方式。Object 实现 Serializablejava view plain copy1. package com.wirelessqa.testintent; 2. 3. import java.io.Serializable; 4. 5. /* 6. * OBJECT实现 SERIALIZABLE 7. * author bixiaopeng 2013-2-18 上午 11:32:19 8. */ 9. public class SerInfo implements Serializable 10

5、. 11. private String name; 12. private String website; 13. private String weibo; 14. public SerInfo() 15. 16. public SerInfo(String name, String website, String weibo) 17. this.name = name; 18. this.website = website; 19. this.weibo = weibo; 20. 21. 22. public String getName() 23. return name; 24. 2

6、5. 26. public void setName(String name) 27. this.name = name; 28. 29. 30. public String getWebsite() 31. return website; 32. 33. 34. public void setWebsite(String website) 35. this.website = website; 36. 37. 38. public String getWeibo() 39. return weibo; 40. 41. 42. public void setWeibo(String weibo

7、) 43. this.weibo = weibo; 44. 45. 46. 用 Serializable 方式传递 Objectjava view plain copy1. SerInfo serInfo = new SerInfo(name, website, weibo); 2. Intent intent = new Intent(); 3. Bundle bundle = new Bundle(); 4. bundle.putSerializable(serinfo, serInfo); 5. intent.setClass(MainActivity.this, ResultActiv

8、ity.class); 6. intent.putExtras(bundle); 7. startActivity(intent); 用 Serializable 方式接收 Objectjava view plain copy1. /获得 Serializable 方式传过来的值 2. SerInfo serInfo = (SerInfo) getIntent().getSerializableExtra(serinfo); 2.2 使用 Parcelable 方式前提:Object 需要实现 Parcelable 接口用 Parcelable 方式传递 Object 的语法:bundle.p

9、utParcelable(key,object);用 Parcelable 方式接收 Object 的语法:object=(Object) getIntent().getParcelableExtra(key);实现 Parcelable 接口的类比较复杂,Parcelable 是个什么东西呢?Android 提供了一种新的类型:Parcel,被用作封装数据的容器,封装后的数据可以通过 Intent 或 IPC 传递。 除了基本类型以外,只有实现了 Parcelable 接口的类才能被放入 Parcel 中。实现 Parcelable 接口需要实现三个方法: 1)writeToParcel 方

10、法。该方法将类的数据写入外部提供的 Parcel 中。声明:writeToParcel(Parcel dest, int flags)。2)describeContents 方法。直接返回 0 就可以。3)静态的 Parcelable.Creator接口,本接口有两个方法:createFromParcel(Parcel in) 实现从 in 中创建出类的实例的功能。newArray(int size) 创 建一个类型为 T,长度为 size 的数组, returnnew Tsize;即可。本方法是供外部类反序列化本类数组使用。Object 需要实现 Parcelable 接口java view

11、 plain copy1. package com.wirelessqa.testintent; 2. 3. import android.os.Parcel; 4. import android.os.Parcelable; 5. 6. /* 7. * Object 需要实现 Parcelable 接口 8. * author bixiaopeng 2013-2-18 上午 11:32:19 9. */ 10. public class ParInfo implements Parcelable 11. private String name; 12. private String webs

12、ite; 13. private String weibo; 14. public ParInfo() 15. 16. 17. public ParInfo(String name, String website, String weibo) 18. this.name = name; 19. this.website = website; 20. this.weibo = weibo; 21. 22. 23. public String getName() 24. return name; 25. 26. 27. public void setName(String name) 28. th

13、is.name = name; 29. 30. 31. public String getWebsite() 32. return website; 33. 34. 35. public void setWebsite(String website) 36. this.website = website; 37. 38. 39. public String getWeibo() 40. return weibo; 41. 42. 43. public void setWeibo(String weibo) 44. this.weibo = weibo; 45. 46. Override 47.

14、 public int describeContents() 48. / TODO Auto-generated method stub 49. return 0; 50. 51. /该方法将类的数据写入外部提供的 Parcel 中。 52. Override 53. public void writeToParcel(Parcel dest, int flags) 54. dest.writeString(name); 55. dest.writeString(website); 56. dest.writeString(weibo); 57. 58. 59. public static final Parcelable.Creator CREATOR = n

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 工业设计

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号