亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android Intent傳遞對象的兩種方法(Serializable,Parcelable)詳細(xì)介紹

 更新時間:2016年12月16日 11:13:47   投稿:lqh  
這篇文章主要介紹了Android Intent傳遞對象的兩種方法(Serializable,Parcelable)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下

Android Intent傳遞對象的兩種方法(Serializable,Parcelable)詳細(xì)介紹

今天要給大家講一下Android中Intent中如何傳遞對象,就我目前所知道的有兩種方法,一種是Bundle.putSerializable(Key,Object);另一種是Bundle.putParcelable(Key, Object);當(dāng)然這些Object是有一定的條件的,前者是實現(xiàn)了Serializable接口,而后者是實現(xiàn)了Parcelable接口,為了讓大家更容易理解我還是照常寫了一個簡單的Demo,大家就一步一步跟我來吧!

第一步:新建一個Android工程命名為ObjectTranDemo(類比較多哦!)目錄結(jié)構(gòu)如下圖:

第二步:修改main.xml布局文件(這里我增加了兩個按鈕)代碼如下

<?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 wei'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>  

第三步:新建兩個類一個是Person.java實現(xiàn)Serializable接口,另一個Book.java實現(xiàn)Parcelable接口,代碼分別如下:

Person.java:

package com.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; 
  } 
   
} 

Book.java:

package com.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<Book> CREATOR = new Creator<Book>() { 
    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對像數(shù)據(jù),和Book對象數(shù)據(jù):,代碼分別如下:

ObjectTranDemo.java:

package com.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(); 
     
  } 
   
  //我的一貫作風(fēng)呵呵 
  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); 
  } 
  //銨鈕點擊事件響應(yīng) 
  public void onClick(View v) { 
    if(v == sButton){ 
      SerializeMethod(); 
    }else{ 
      PacelableMethod(); 
    } 
  } 
} 

ObjectTranDemo1.java:

package com.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() + "/n"+ 
        "You age is: " + mPerson.getAge()); 
     
    setContentView(mTextView); 
  } 
} 

ObjectTranDemo2.java:

package com.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()+"/n"+ 
             "Author is: " + mBook.getAuthor() + "/n" + 
             "PublishTime is: " + mBook.getPublishTime()); 
    setContentView(mTextView); 
  } 
} 

第五步:比較重要的一步啦,修改AndroidManifest.xml文件(將兩個新增的Activity,ObjectTranDemo1,ObjectTranDemo2)申明一下代碼如下(第14,15行):

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="com.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=".ObjectTranDemo1"></activity> 
    <activity android:name=".ObjectTranDemo2"></activity> 
  </application> 
  <uses-sdk android:minSdkVersion="7" /> 
</manifest>  

第六步:運行上述工程查看效果圖啦:

效果1:首界面:

效果2:點擊Serializable按鈕

效果3:點擊Parcelable按鈕:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Android圓角設(shè)置方法看著一篇文章就夠了

    Android圓角設(shè)置方法看著一篇文章就夠了

    我們在實際工作中,android經(jīng)常有需要實現(xiàn)圓角的場景,下面這篇文章主要給大家介紹了關(guān)于Android圓角設(shè)置方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì)需要的朋友可以參考下
    2023-02-02
  • 仿iPhone風(fēng)格對話框(附件包含例子/jar包/jar包源碼)

    仿iPhone風(fēng)格對話框(附件包含例子/jar包/jar包源碼)

    這個對框完全繼承、仿照AlertDialog,只是實現(xiàn)了自定義效果;另外,沒有實現(xiàn)setIcon,因為iphone中的對話框多數(shù)都沒有圖標(biāo);附件包含例子、jar包、jar包源碼
    2013-01-01
  • 詳解Android中Service AIDL的使用

    詳解Android中Service AIDL的使用

    作為一名Android開發(fā)人員,如果沒聽過Service,那就有點說不過去了啊,Service是Android四大組件之一,它是不依賴于用戶界面的,就是因為Service不依賴與用戶界面,所以我們常常用于進(jìn)行一些耗時的操作,比如:下載數(shù)據(jù)等;本文將詳細(xì)介紹Android中Service AIDL的使用。
    2021-06-06
  • Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/下載超時的問題

    Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/

    Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時 親測有效解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-12-12
  • flutter局部刷新的實現(xiàn)示例

    flutter局部刷新的實現(xiàn)示例

    這篇文章主要介紹了flutter局部刷新的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Android自定義View實現(xiàn)字母導(dǎo)航欄的代碼

    Android自定義View實現(xiàn)字母導(dǎo)航欄的代碼

    這篇文章主要介紹了Android自定義View實現(xiàn)字母導(dǎo)航欄的實例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Flutter?GetX使用實例詳細(xì)解讀

    Flutter?GetX使用實例詳細(xì)解讀

    這篇文章主要為大家介紹了Flutter?GetX使用示例詳細(xì)解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例

    Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例

    GridView布局比較基礎(chǔ),可以取代已經(jīng)逐漸淡出人們視線的TableLayout,這里我們就來看一下Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例:
    2016-06-06
  • 淺談Android面向切面編程(AOP)

    淺談Android面向切面編程(AOP)

    這篇文章主要介紹了淺談Android面向切面編程(AOP),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android BroadcastReceiver廣播注冊方式總結(jié)

    Android BroadcastReceiver廣播注冊方式總結(jié)

    這篇文章主要介紹了Android BroadcastReceiver廣播注冊方式總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評論