最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android系列之Intent傳遞對象的幾種實例方法

 更新時間:2013年05月29日 10:27:18   作者:  
Android系列之Intent傳遞對象的幾種實例方法,需要的朋友可以參考一下

 在Android中intent傳遞對象主要有2種方式分別是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key, Object);當然這些Object是有一定的條件的,前者是實現(xiàn)了Serializable接口,而后者是實現(xiàn)了Parcelable接口,以下是我為大家做的一個實例
  首先我們建立一個工程項目命名為:ObjectTestDemo
  然后我們再修改main.xml布局文件,主要增加2個按鈕
view plaincopy to clipboardprint?

復(fù)制代碼 代碼如下:

  < ?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]
接下來我們開始對工程進行實現(xiàn),分別建立Person.java實現(xiàn)Serializable接口,另一個Book.java實現(xiàn)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;

 }

 }

復(fù)制代碼 代碼如下:

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;

 }

 }

復(fù)制代碼 代碼如下:

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);

 }

 }

復(fù)制代碼 代碼如下:

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對像數(shù)據(jù),和Book對象數(shù)據(jù)

代碼

復(fù)制代碼 代碼如下:

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);

   }

   //銨鈕點擊事件響應(yīng)

   public void onClick(View v) {

   if(v == sButton){

   SerializeMethod();

  }else{

  PacelableMethod();

  }

  }

  }

代碼

復(fù)制代碼 代碼如下:

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);

 }

 //銨鈕點擊事件響應(yīng)

 public void onClick(View v) {

 if(v == sButton){

 SerializeMethod();

 }else{

 PacelableMethod();

 }

 }

 }


代碼
復(fù)制代碼 代碼如下:

 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);

   }


代碼
復(fù)制代碼 代碼如下:

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);

 }

 }


代碼
復(fù)制代碼 代碼如下:

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);

 }

 }

復(fù)制代碼 代碼如下:

 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);

   }

   }

下面是最重要的環(huán)節(jié):修改AndroidManifest.xml文件(將兩個新增的Activity,ObjecttestDemo1,ObjecttestDemo2)申明一下代碼如下(第14,15行):

代碼

復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • Android10 分區(qū)存儲的適配規(guī)則

    Android10 分區(qū)存儲的適配規(guī)則

    Android設(shè)備 在Sdk29己以上推出了分區(qū)存儲,類似iOS沙箱。應(yīng)用App 目標版本號為29或以上則需要適配分區(qū)存儲,本文將具體的講解適配規(guī)則
    2021-05-05
  • Android仿微信輸入框效果的實現(xiàn)代碼

    Android仿微信輸入框效果的實現(xiàn)代碼

    這篇文章主要介紹了Android仿微信輸入框效果的實現(xiàn)代碼,需要的朋友參考下吧
    2017-05-05
  • Kotlin中使用Dagger2可能遇到的坑解決

    Kotlin中使用Dagger2可能遇到的坑解決

    在Android上創(chuàng)建去耦以及容易測試代碼的幾乎每位遲早都要訴諸Dagger,在Kotlin中設(shè)置Dagger有一些不同,所以下面這篇文章主要給大家介紹了關(guān)于Kotlin中使用Dagger2可能遇到的坑的解決方法,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-11-11
  • Android 實現(xiàn)圓圈擴散水波動畫效果兩種方法

    Android 實現(xiàn)圓圈擴散水波動畫效果兩種方法

    這篇文章主要介紹了Android 實現(xiàn)圓圈擴散水波動畫效果兩種方法,需要的朋友可以參考下
    2018-05-05
  • Android SharedPreferences數(shù)據(jù)存儲詳解

    Android SharedPreferences數(shù)據(jù)存儲詳解

    SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出
    2022-11-11
  • Android webveiw 出現(xiàn)棧錯誤解決辦法

    Android webveiw 出現(xiàn)棧錯誤解決辦法

    這篇文章主要介紹了Android webveiw 出現(xiàn)棧錯誤解決辦法的相關(guān)資料,出現(xiàn)java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes,這里提供解決辦法,需要的朋友可以參考下
    2017-08-08
  • Android中閃屏實現(xiàn)方法小結(jié)(普通閃屏、倒計時閃屏、倒計時+動畫閃屏)

    Android中閃屏實現(xiàn)方法小結(jié)(普通閃屏、倒計時閃屏、倒計時+動畫閃屏)

    這篇文章主要介紹了Android中閃屏實現(xiàn)方法小結(jié)(普通閃屏、倒計時閃屏、倒計時+動畫閃屏),非常不錯,代碼簡單易懂,需要的朋友可以參考下
    2017-01-01
  • Android自定義文件路徑選擇器

    Android自定義文件路徑選擇器

    這篇文章主要為大家詳細介紹了Android自定義文件路徑選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 關(guān)于Android bitmap你不知道的一些事

    關(guān)于Android bitmap你不知道的一些事

    這篇文章主要為大家詳細介紹了關(guān)于Android bitmap你不知道的一些事,使用bitmap需要注意的一些細節(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • android?WindowManager的簡單使用實例詳解

    android?WindowManager的簡單使用實例詳解

    這篇文章主要介紹了android?WindowManager的簡單使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08

最新評論

永丰县| 阿拉尔市| 叶城县| 屯留县| 东乡| 博爱县| 渝北区| 汝城县| 高安市| 岳阳市| 三台县| 连城县| 钦州市| 南和县| 七台河市| 张家港市| 洞口县| 乌兰浩特市| 铁岭市| 梨树县| 光山县| 沙河市| 苏尼特右旗| 株洲县| 西宁市| 潮安县| 汉中市| 北海市| 攀枝花市| 浦城县| 肇庆市| 铜川市| 扎鲁特旗| 洛南县| 安阳县| 密山市| 富平县| 从化市| 和平区| 普格县| 新巴尔虎右旗|