Android系列之Intent传递对象的几种实例方法

Android系列之Intent传递对象的几种实例方法,第1张

概述 在Android中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key,Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcela

 在AndroID中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key,Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,以下是我为大家做的一个实例
  首先我们建立一个工程项目命名为:ObjectTestDemo
  然后我们再修改main.xml布局文件,主要增加2个按钮
vIEw plaincopy to clipboardprint?
复制代码 代码如下:
  < ?xml version="1.0" enCoding="utf-8"?>

  < linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"

  androID:orIEntation="vertical"

  androID:layout_wIDth="fill_parent"

   androID:layout_height="fill_parent"

   >

   < TextVIEw

   androID:layout_wIDth="fill_parent"

   androID:layout_height="wrap_content"

   androID:text="Welcome to Mr Jesson's blog."

   />

   < button

   androID:ID="@+ID/button1"

   androID:layout_wIDth="fill_parent"

   androID:layout_height="wrap_content"

   androID:text="Serializable"

   />

   < button

   androID:ID="@+ID/button2"

   androID:layout_wIDth="fill_parent"

   androID:layout_height="wrap_content"

   androID:text="Parcelable"

   />

   < /linearLayout>

   < ?xml version="1.0" enCoding="utf-8"?>

   < linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"

   androID:orIEntation="vertical"

   androID:layout_wIDth="fill_parent"

   androID:layout_height="fill_parent"

   >

   < TextVIEw

   androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content"

   androID:text="Welcome to Mr jesson's blog."

   />

   < button

   androID:ID="@+ID/button1"

   androID:layout_wIDth="fill_parent"

   androID:layout_height="wrap_content"

   androID:text="Serializable"

   />

   < button

   androID:ID="@+ID/button2"

   androID:layout_wIDth="fill_parent"

   androID:layout_height="wrap_content"

   androID:text="Parcelable"

   />

   < /linearLayout>
  [code]
接下来我们开始对工程进行实现,分别建立Person.java实现Serializable接口,另一个Book.java实现Parcelable接口

[code]
package com.test.objecttran;

import java.io.Serializable;

public class Person implements Serializable {

private static final long serialVersionUID = -7060210544600464481L;

private String name;

 private int age;

 public String getname() {

 return name;

 }

 public voID setname(String name) {

 this.name = name;

 }

 public int getAge() {

 return age;

 }

 public voID setAge(int age) {

 this.age = age;

 }

 }

复制代码 代码如下:
package com.test.tutor.objecttran;

import java.io.Serializable;

public class Person implements Serializable {

private static final long serialVersionUID = -7060210544600464481L;

private String name;

 private int age;

 public String getname() {

 return name;

 }

 public voID setname(String name) {

 this.name = name;

 }

 public int getAge() {

 return age;

 }

 public voID setAge(int age) {

 this.age = age;

 }

 }

复制代码 代码如下:
package com.test.tutor.objecttran;

 

import androID.os.Parcel;

import androID.os.Parcelable;

public class Book implements Parcelable {

 private String bookname;

 private String author;

 private int publishTime;

 public String getBookname() {

 return bookname;

 }

 public voID setBookname(String bookname) {

 this.bookname = bookname;

 }

 public String getAuthor() {

 return author;

 }

 public voID setAuthor(String author) {

 this.author = author;

 }

 public int getPublishTime() {

 return publishTime;

 }

 public voID setPublishTime(int publishTime) {

 this.publishTime = publishTime;

 }

 public static final Parcelable.Creator CREATOR = new Creator() {

 public Book createFromParcel(Parcel source) {

 Book mBook = new Book();

 mBook.bookname = source.readString();

 mBook.author = source.readString();

 mBook.publishTime = source.readInt();

 return mBook;

 }

 public Book[] newArray(int size) {

 return new Book[size];

 }

 };

 public int describeContents() {

 return 0;

 }

 public voID writetoParcel(Parcel parcel,int flags) {

 parcel.writeString(bookname);

 parcel.writeString(author);

 parcel.writeInt(publishTime);

 }

 }

复制代码 代码如下:
package com.test.tutor.objecttran;

import androID.os.Parcel;

import androID.os.Parcelable;

public class Book implements Parcelable {

private String bookname;

 private String author;

 private int publishTime;

 public String getBookname() {

 return bookname;

 }

 public voID setBookname(String bookname) {this.bookname = bookname;

 }

 public String getAuthor() {

 return author;

 }

 public voID setAuthor(String author) {

 this.author = author;

 }

 public int getPublishTime() {

 return publishTime;

 }

 public voID setPublishTime(int publishTime) {

 this.publishTime = publishTime;

 }

 public static final Parcelable.Creator CREATOR = new Creator() {

 public Book createFromParcel(Parcel source) {

 Book mBook = new Book();

 mBook.bookname = source.readString();

 mBook.author = source.readString();

 mBook.publishTime = source.readInt();

 return mBook;

 }

 public Book[] newArray(int size) {

 return new Book[size];

 }

 };

 public int describeContents() {

 return 0;

 }

 public voID writetoParcel(Parcel parcel,int flags) {

 parcel.writeString(bookname);

 parcel.writeString(author);

 parcel.writeInt(publishTime);

 }

 }


修改ObjectTranDemo.java,并且新建两个Activity,一个是ObjectTranDemo1.java,别一个是ObjectTranDemo2.java.分别用来显示Person对像数据,和Book对象数据

代码

复制代码 代码如下:
package com.test.tutor.objecttran;

     import androID.app.Activity;

     import androID.content.Intent;

     import androID.os.Bundle;

     import androID.vIEw.VIEw;

   import androID.vIEw.VIEw.OnClickListener;

   import androID.Widget.button;

   public class ObjectTranDemo extends Activity implements OnClickListener {

   private button sbutton,pbutton;

   public final static String SER_KEY = "com.tutor.objecttran.ser";

   public final static String PAR_KEY = "com.tutor.objecttran.par";

 public voID onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentVIEw(R.layout.main);

   setupVIEws();

   }

 
   public voID setupVIEws(){

   sbutton = (button)findVIEwByID(R.ID.button1);

   pbutton = (button)findVIEwByID(R.ID.button2);

   sbutton.setonClickListener(this);

   pbutton.setonClickListener(this);

   }

   //Serializeable传递对象的方法

   public voID SerializeMethod(){

   Person mPerson = new Person();

   mPerson.setname("frankIE");

   mPerson.setAge(25);

   Intent mIntent = new Intent(this,ObjectTranDemo1.class);

   Bundle mBundle = new Bundle();

   mBundle.putSerializable(SER_KEY,mPerson);

   mIntent.putExtras(mBundle);

   startActivity(mIntent);

   }

   //Pacelable传递对象方法

   public voID PacelableMethod(){

   Book mBook = new Book();

   mBook.setBookname("AndroID Tutor");

   mBook.setAuthor("FrankIE");

   mBook.setPublishTime(2010);

   Intent mIntent = new Intent(this,ObjectTranDemo2.class);

   Bundle mBundle = new Bundle();

   mBundle.putParcelable(PAR_KEY,mBook);

   mIntent.putExtras(mBundle);

   startActivity(mIntent);

   }

   //铵钮点击事件响应

   public voID onClick(VIEw v) {

   if(v == sbutton){

   SerializeMethod();

  }else{

  PacelableMethod();

  }

  }

  }

代码
复制代码 代码如下:
package com.test.tutor.objecttran;

import androID.app.Activity;

import androID.content.Intent;

import androID.os.Bundle;

import androID.vIEw.VIEw;

 import androID.vIEw.VIEw.OnClickListener;

 import androID.Widget.button;

 public class ObjectTranDemo extends Activity implements OnClickListener {

 

 private button sbutton,pbutton;

 public final static String SER_KEY = "com.tutor.objecttran.ser";

 public final static String PAR_KEY = "com.tutor.objecttran.par";

 public voID onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentVIEw(R.layout.main);

 setupVIEws();

 }

 public voID setupVIEws(){

 sbutton = (button)findVIEwByID(R.ID.button1);

 pbutton = (button)findVIEwByID(R.ID.button2);

 sbutton.setonClickListener(this);

 pbutton.setonClickListener(this);

 }

 //Serializeable传递对象的方法

 public voID SerializeMethod(){

 Person mPerson = new Person();

 mPerson.setname("frankIE");

 mPerson.setAge(25);

 Intent mIntent = new Intent(this,ObjectTranDemo1.class);

 Bundle mBundle = new Bundle();

 mBundle.putSerializable(SER_KEY,mPerson);

 mIntent.putExtras(mBundle);

 startActivity(mIntent);

 }

 //Pacelable传递对象方法

 public voID PacelableMethod(){

 Book mBook = new Book();

 mBook.setBookname("AndroID Tutor");

 mBook.setAuthor("FrankIE");

 mBook.setPublishTime(2010);

 Intent mIntent = new Intent(this,ObjectTranDemo2.class);

 Bundle mBundle = new Bundle();

 mBundle.putParcelable(PAR_KEY,mBook);

 mIntent.putExtras(mBundle);

 startActivity(mIntent);

 }

 //铵钮点击事件响应

 public voID onClick(VIEw v) {

 if(v == sbutton){

 SerializeMethod();

 }else{

 PacelableMethod();

 }

 }

 }


代码
复制代码 代码如下:
 package com.test.tutor.objecttran;

    import androID.app.Activity;
      import androID.os.Bundle;
     import androID.Widget.TextVIEw;

    public class ObjectTranDemo1 extends Activity {

    @OverrIDe

   public voID onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

   TextVIEw mTextVIEw = new TextVIEw(this);

   Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);

   mTextVIEw.setText("You name is: " + mPerson.getname() + ""+

   "You age is: " + mPerson.getAge());

   setContentVIEw(mTextVIEw);

   }

代码
复制代码 代码如下:
package com.test.tutor.objecttran;

import androID.app.Activity;

import androID.os.Bundle;

import androID.Widget.TextVIEw;

public class ObjectTranDemo1 extends Activity {

 @OverrIDe

 public voID onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 TextVIEw mTextVIEw = new TextVIEw(this);

 Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);

 mTextVIEw.setText("You name is: " + mPerson.getname() + ""+

 "You age is: " + mPerson.getAge());

 setContentVIEw(mTextVIEw);

 }

 }


代码
复制代码 代码如下:
package com.test.tutor.objecttran;

import androID.app.Activity;

import androID.os.Bundle;

import androID.Widget.TextVIEw;

public class ObjectTranDemo2 extends Activity {

 public voID onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 TextVIEw mTextVIEw = new TextVIEw(this);

 Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);

 mTextVIEw.setText("Book name is: " + mBook.getBookname()+""+

 "Author is: " + mBook.getAuthor() + "" +

 "PublishTime is: " + mBook.getPublishTime()); setContentVIEw(mTextVIEw);

 }

 }

复制代码 代码如下:
 package com.test.tutor.objecttran;

  import androID.app.Activity;

  import androID.os.Bundle;

  import androID.Widget.TextVIEw;

  public class ObjectTranDemo2 extends Activity {

   public voID onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   TextVIEw mTextVIEw = new TextVIEw(this);

   Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);

   mTextVIEw.setText("Book name is: " + mBook.getBookname()+""+

   "Author is: " + mBook.getAuthor() + "" +

   "PublishTime is: " + mBook.getPublishTime());

   setContentVIEw(mTextVIEw);

   }

   }

下面是最重要的环节:修改AndroIDManifest.xml文件(将两个新增的Activity,ObjecttestDemo1,ObjecttestDemo2)申明一下代码如下(第14,15行):

代码
复制代码 代码如下:
< ?xml version="1.0" enCoding="utf-8"?>

< manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"

package="com.test.tutor.objecttran"

androID:versionCode="1"

androID:versionname="1.0">

 < application androID:icon="@drawable/icon" androID:label="@string/app_name">

 < activity androID:name=".ObjectTranDemo"

 androID:label="@string/app_name">

 < intent-filter>

 < action androID:name="androID.intent.action.MAIN" />

 < category androID:name="androID.intent.category.LAUNCHER" />

 < /intent-filter>

 < /activity>

 < activity androID:name=".ObjecttestDemo1">< /activity>

 < activity androID:name=".ObjecttestDemo2">< /activity>

 < /application>

 < uses-sdk androID:minSdkVersion="7" />

 < /manifest>

 < ?xml version="1.0" enCoding="utf-8"?>

 < manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"

 package="com.test.tutor.objecttran"

 androID:versionCode="1"

 androID:versionname="1.0">

 < application androID:icon="@drawable/icon" androID:label="@string/app_name">

 < activity androID:name=".ObjectTranDemo"

 androID:label="@string/app_name">

 < intent-filter>

 < action androID:name="androID.intent.action.MAIN" />

 < category androID:name="androID.intent.category.LAUNCHER" />

 < /intent-filter>

 < /activity>

 < activity androID:name=".ObjecttestDemo1">< /activity>

 < activity androID:name=".ObjecttestDemo2">< /activity>

 < /application>

 < uses-sdk androID:minSdkVersion="7" />

 < /manifest>

总结

以上是内存溢出为你收集整理的Android系列之Intent传递对象的几种实例方法全部内容,希望文章能够帮你解决Android系列之Intent传递对象的几种实例方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1141443.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存