zoukankan      html  css  js  c++  java
  • Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口 

    第一步:新建一个Android工程命名为IntentDemo 

    第二步:修改main.xml布局文件 

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    3.     android:orientation="vertical"    
    4.     android:layout_width="fill_parent"    
    5.     android:layout_height="fill_parent"    
    6.     >    
    7. <TextView      
    8.     android:layout_width="fill_parent"     
    9.     android:layout_height="wrap_content"     
    10.     android:text="Welcome to Mr wei's blog."    
    11.     />    
    12. <Button    
    13.     android:id="@+id/button1"    
    14.     android:layout_width="fill_parent"    
    15.     android:layout_height="wrap_content"    
    16.     android:text="Serializable"    
    17. />    
    18. <Button    
    19.     android:id="@+id/button2"    
    20.     android:layout_width="fill_parent"    
    21.     android:layout_height="wrap_content"    
    22.     android:text="Parcelable"    
    23. />    
    24. </LinearLayout>     



    第三步:新建两个类一个是Person.java实现Serializable接口,另一个Book.java实现Parcelable接口,代码分别如下: 
    Person.java 

    Java代码  收藏代码
    1. package cn.caiwb.intent;    
    2. import java.io.Serializable;    
    3. public class Person implements Serializable {    
    4.    
    5.     private String name;    
    6.     private int age;    
    7.     public String getName() {    
    8.         return name;    
    9.     }    
    10.     public void setName(String name) {    
    11.         this.name = name;    
    12.     }    
    13.     public int getAge() {    
    14.         return age;    
    15.     }    
    16.     public void setAge(int age) {    
    17.         this.age = age;    
    18.     }    
    19.         
    20. }    


    Book.java 

    Java代码  收藏代码
    1. package cn.caiwb.intent;    
    2. import android.os.Parcel;    
    3. import android.os.Parcelable;    
    4. public class Book implements Parcelable {    
    5.     private String bookName;    
    6.     private String author;    
    7.     private int publishTime;    
    8.         
    9.     public String getBookName() {    
    10.         return bookName;    
    11.     }    
    12.     public void setBookName(String bookName) {    
    13.         this.bookName = bookName;    
    14.     }    
    15.     public String getAuthor() {    
    16.         return author;    
    17.     }    
    18.     public void setAuthor(String author) {    
    19.         this.author = author;    
    20.     }    
    21.     public int getPublishTime() {    
    22.         return publishTime;    
    23.     }    
    24.     public void setPublishTime(int publishTime) {    
    25.         this.publishTime = publishTime;    
    26.     }    
    27.         
    28.     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {    
    29.         public Book createFromParcel(Parcel source) {    
    30.             Book mBook = new Book();    
    31.             mBook.bookName = source.readString();    
    32.             mBook.author = source.readString();    
    33.             mBook.publishTime = source.readInt();    
    34.             return mBook;    
    35.         }    
    36.         public Book[] newArray(int size) {    
    37.             return new Book[size];    
    38.         }    
    39.     };    
    40.         
    41.     public int describeContents() {    
    42.         return 0;    
    43.     }    
    44.     public void writeToParcel(Parcel parcel, int flags) {    
    45.         parcel.writeString(bookName);    
    46.         parcel.writeString(author);    
    47.         parcel.writeInt(publishTime);    
    48.     }    
    49. }    



    第四步:修改IntentDemo.java,并且新建两个Activity,一个是IntentDemo1.java,别一个是IntentDemo2.java.分别用来显示Person对像数据,和Book对象数据:,代码分别如下: 

    IntentDemo.java 

    Java代码  收藏代码
    1. package cn.caiwb.intent;    
    2. import android.app.Activity;    
    3. import android.content.Intent;    
    4. import android.os.Bundle;    
    5. import android.view.View;    
    6. import android.view.View.OnClickListener;    
    7. import android.widget.Button;    
    8. public class IntentDemo extends Activity implements OnClickListener {    
    9.         
    10.     private Button sButton,pButton;    
    11.     public  final static String SER_KEY = "cn.caiwb.intent.ser";    
    12.     public  final static String PAR_KEY = "cn.caiwb.intent.par";    
    13.     public void onCreate(Bundle savedInstanceState) {    
    14.         super.onCreate(savedInstanceState);    
    15.         setContentView(R.layout.main);       
    16.         setupViews();    
    17.             
    18.     }    
    19.         
    20.     public void setupViews(){    
    21.         sButton = (Button)findViewById(R.id.button1);    
    22.         pButton = (Button)findViewById(R.id.button2);    
    23.         sButton.setOnClickListener(this);    
    24.         pButton.setOnClickListener(this);    
    25.     }    
    26.     //Serializeable传递对象的方法    
    27.     public void SerializeMethod(){    
    28.         Person mPerson = new Person();    
    29.         mPerson.setName("frankie");    
    30.         mPerson.setAge(25);    
    31.         Intent mIntent = new Intent(this,IntentDemo1.class);    
    32.         Bundle mBundle = new Bundle();    
    33.         mBundle.putSerializable(SER_KEY,mPerson);    
    34.         mIntent.putExtras(mBundle);    
    35.             
    36.         startActivity(mIntent);    
    37.     }    
    38.     //Pacelable传递对象方法    
    39.     public void PacelableMethod(){    
    40.         Book mBook = new Book();    
    41.         mBook.setBookName("Android Tutor");    
    42.         mBook.setAuthor("Frankie");    
    43.         mBook.setPublishTime(2010);    
    44.         Intent mIntent = new Intent(this,IntentDemo2.class);    
    45.         Bundle mBundle = new Bundle();    
    46.         mBundle.putParcelable(PAR_KEY, mBook);    
    47.         mIntent.putExtras(mBundle);    
    48.             
    49.         startActivity(mIntent);    
    50.     }    
    51.     //铵钮点击事件响应    
    52.     public void onClick(View v) {    
    53.         if(v == sButton){    
    54.             SerializeMethod();    
    55.         }else{    
    56.             PacelableMethod();    
    57.         }    
    58.     }    
    59. }    



    IntentDemo1.java 

    Java代码  收藏代码
    1. package cn.caiwb.intent;    
    2. import android.app.Activity;    
    3. import android.os.Bundle;    
    4. import android.widget.TextView;    
    5. public class IntentDemo1 extends Activity {    
    6.     @Override    
    7.     public void onCreate(Bundle savedInstanceState) {    
    8.         super.onCreate(savedInstanceState);    
    9.             
    10.         TextView mTextView = new TextView(this);    
    11.         Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);    
    12.         mTextView.setText("You name is: " + mPerson.getName() + "/n"+    
    13.                 "You age is: " + mPerson.getAge());    
    14.             
    15.         setContentView(mTextView);    
    16.     }    
    17. }    



    IntentDemo2.java 

    Java代码  收藏代码
    1. package cn.caiwb.intent;    
    2. import android.app.Activity;    
    3. import android.os.Bundle;    
    4. import android.widget.TextView;    
    5. public class IntentDemo2 extends Activity {    
    6.      
    7.     public void onCreate(Bundle savedInstanceState) {    
    8.         super.onCreate(savedInstanceState);    
    9.         TextView mTextView = new TextView(this);    
    10.         Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);    
    11.         mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+    
    12.                           "Author is: " + mBook.getAuthor() + "/n" +    
    13.                           "PublishTime is: " + mBook.getPublishTime());    
    14.         setContentView(mTextView);    
    15.     }    
    16. }    



    OK 可以了~ 

    转自:链接

  • 相关阅读:
    Android Activity与Service的交互方式
    Android Service和Thread的区别
    Android Binder机制简单了解
    Android内的生命周期整理
    Android App的生命周期是什么
    ListView item 中TextView 如何获取长按事件
    Go之并发处理(售票问题)
    Go之简单并发
    Go之函数直接实现接口
    Go之类型判断
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/4923949.html
Copyright © 2011-2022 走看看