广告
返回顶部
首页 > 资讯 > 移动开发 >Android系列之Intent传递对象的几种实例方法
  • 876
分享到

Android系列之Intent传递对象的几种实例方法

方法对象intent传递对象intentAndroid 2022-06-06 10:06:33 876人浏览 泡泡鱼
摘要

 在Android中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(K

 在Android中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,以下是我为大家做的一个实例
  首先我们建立一个工程项目命名为:ObjectTestDemo
  然后我们再修改main.xml布局文件,主要增加2个按钮
view plaincopy to clipboardprint?
代码如下:
  < ?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]
接下来我们开始对工程进行实现,分别建立Person.java实现Serializable接口,另一个Book.java实现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;
 }
 }

代码如下:
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;
 }
 }

代码如下:
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);
 }
 }

代码如下:
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对像数据,和Book对象数据

代码

代码如下:
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);
   }
   //铵钮点击事件响应
   public void onClick(View v) {
   if(v == sButton){
   SerializeMethod();
  }else{

  PacelableMethod();

  }

  }

  }

代码
代码如下:
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);
 }
 //铵钮点击事件响应
 public void onClick(View v) {
 if(v == sButton){
 SerializeMethod();
 }else{
 PacelableMethod();
 }
 }
 }

代码
代码如下:
 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);
   }

代码
代码如下:
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);
 }
 }

代码
代码如下:
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);
 }
 }

代码如下:
 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);
   }
   }

下面是最重要的环节:修改AndroidManifest.xml文件(将两个新增的Activity,ObjecttestDemo1,ObjecttestDemo2)申明一下代码如下(第14,15行):

代码
代码如下:
< ?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>

您可能感兴趣的文章:android中intent传递list或者对象的方法Android中Intent传递对象的3种方式详解Android 通过Intent使用Bundle传递对象详细介绍Android 使用Intent传递数据的实现思路与代码Android开发之利用Intent实现数据传递的方法详解Android中Intent传递对象给Activity的方法Android编程使用Intent传递图片的方法详解Android Intent传递数据大小限制详解


--结束END--

本文标题: Android系列之Intent传递对象的几种实例方法

本文链接: https://www.lsjlt.com/news/27643.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作