国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法

2020-04-11 10:55:56
字體:
供稿:網(wǎng)友

 Android中的不同Activity之間傳遞對象,我們可以考慮采用Bundle.putSerializable(Key,Object);也可以考慮采用Bundle.putParcelable(Key, Object);其中前面一種方法中的Object要實(shí)現(xiàn)Serializable接口,后面一種方法中的Object要實(shí)現(xiàn)Parcelable接口。下面我們以一個(gè)完整的例子來說明。

1.新建一個(gè)Android的工程,其中該工程的目錄結(jié)構(gòu)如下圖:

2. 修改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="@string/hello" /> <Button android:id="@+id/serButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Serializable"/> <Button android:id="@+id/parButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Parcelable"/> </LinearLayout> 

3.在工程的src目錄下新建一個(gè)實(shí)體類包,命名為com.andy.entity.同時(shí)在該package中添加兩個(gè)實(shí)體類,一個(gè)是Person.java,該類實(shí)現(xiàn)Serializable接口;一個(gè)是Police.java,該類實(shí)現(xiàn)Parcelable接口。代碼分別如下:

Person.java:package com.andy.entity; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = -6919461967497580385L; 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; } } Police.java:[c-sharp] view plain copypackage com.andy.entity; import android.os.Parcel; import android.os.Parcelable; public class Police implements Parcelable { private String name; private int workTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWorkTime() { return workTime; } public void setWorkTime(int workTime) { this.workTime = workTime; } public static final Parcelable.Creator<Police> CREATOR = new Creator<Police>() { @Override public Police createFromParcel(Parcel source) { Police police = new Police(); police.name = source.readString(); police.workTime = source.readInt(); return police; } @Override public Police[] newArray(int size) { return new Police[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(name); parcel.writeInt(workTime); } }

4.在包c(diǎn)om.andy.testdemo中修改TestActivity.java類,同時(shí)在該包中添加類SerializableDemo和ParcelableDemo,分別繼承了Activity類和分別顯示Person對象和Police對象的數(shù)據(jù)。代碼如下:

package com.andy.testdemo; import com.andy.entity.Person; import com.andy.entity.Police; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class TestActivity extends Activity { private Button sButton,pButton; public final static String SER_KEY = "com.andy.ser"; public final static String PAR_KEY = "com.andy.par"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sButton = (Button)findViewById(R.id.serButton); sButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SerializeMethod(); } }); pButton = (Button)findViewById(R.id.parButton); pButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PacelableMethod(); } }); } /** * Serializeable傳遞對象的方法 */ private void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName("andy"); mPerson.setAge(26); Intent mIntent = new Intent(this,SerializableDemo.class); Bundle mBundle = new Bundle(); mBundle.putSerializable(SER_KEY,mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } /** * Pacelable傳遞對象方法 */ private void PacelableMethod(){ Police mPolice = new Police(); mPolice.setName("I am Police"); mPolice.setWorkTime(2008); Intent mIntent = new Intent(this,ParcelableDemo.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(PAR_KEY, mPolice); mIntent.putExtras(mBundle); startActivity(mIntent); } }

SerializableDemo.java類

package com.andy.testdemo; import com.andy.entity.Person; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SerializableDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Person mPerson = (Person)getIntent().getSerializableExtra(TestActivity.SER_KEY); mTextView.setText("You name is: " + mPerson.getName() + "/n"+ "You age is: " + mPerson.getAge()); setContentView(mTextView); } }

ParcelableDemo.java類:

package com.andy.testdemo; import com.andy.entity.Police; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ParcelableDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Police mPolice = (Police)getIntent().getParcelableExtra(TestActivity.PAR_KEY); mTextView.setText("Police name is: " + mPolice.getName()+"/n"+ "WorkTime is: " + mPolice.getWorkTime() + "/n"); setContentView(mTextView); } } 

5.在AndroidManifest.xml文件中為新添加的兩個(gè)Activity進(jìn)行注冊。

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.andy.testdemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestActivity" 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=".SerializableDemo"/> <activity android:name=".ParcelableDemo"/> </application> <uses-sdk android:minSdkVersion="8" /> </manifest> 

6.運(yùn)行程序查看效果圖:

【1】主界面截圖:


【2】點(diǎn)擊Serializable按鈕的效果


【3】點(diǎn)擊Parcelable按鈕的效果

=========================================================================
以上是如何采用Intent在不同的Activity之間傳遞對象的例子。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 嵊州市| 鄄城县| 鹿泉市| 玉溪市| 荣昌县| 吴川市| 江陵县| 育儿| 汝阳县| 西昌市| 德州市| 武鸣县| 高唐县| 多伦县| 东阳市| 台东县| 南通市| 滁州市| 进贤县| 康平县| 宁乡县| 伊金霍洛旗| 台南市| 大庆市| 高台县| 灵璧县| 冕宁县| 湟源县| 林芝县| 林芝县| 门源| 菏泽市| 进贤县| 郸城县| 江口县| 会同县| 黄山市| 新乐市| 武陟县| 陇南市| 乐平市|