Android系列之Intent傳遞對象的幾種執行個體方法

來源:互聯網
上載者:User

 在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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.