Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6509字,纯文字阅读大概需要10分钟。
内容图文
http://blog.csdn.net/cjjky/article/details/6441104
在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSerializable(Key,Object);也可以考虑采用Bundle.putParcelable(Key, Object);其中前面一种方法中的Object要实现Serializable接口,后面一种方法中的Object要实现Parcelable接口。下面我们以一个完整的例子来说明。
1.新建一个Android的工程,其中该工程的目录结构如下图:
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目录下新建一个实体类包,命名为com.andy.entity.同时在该package中添加两个实体类,一个是Person.Java,该类实现Serializable接口;一个是Police.java,该类实现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:
- package 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.在包com.andy.testdemo中修改TestActivity.java类,同时在该包中添加类SerializableDemo和ParcelableDemo,分别继承了Activity类和分别显示Person对象和Police对象的数据。代码如下:
- 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文件中为新添加的两个Activity进行注册。
- <?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.运行程序查看效果图:
【1】主界面截图:
【2】点击Serializable按钮的效果
【3】点击Parcelable按钮的效果
=========================================================================
以上是如何采用Intent在不同的Activity之间传递对象的例子。
原文:http://www.cnblogs.com/feng9exe/p/5954267.html
内容总结
以上是互联网集市为您收集整理的Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]全部内容,希望文章能够帮你解决Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。