Intent传递对象的两种方式(putSerializable,putParelable)

来源:互联网 发布:新网互联域名证书 编辑:程序博客网 时间:2024/04/30 13:19

1 第一种方式(putSerializable)

这种方式相对于第一种要简单的多,使用的也比较常见

创建一个类user实现Serializable接口(只需写上成员变量以及相应的getXxx()和setXxx()),这里就不多说了

在MainActivity中定义一个按钮变量btn1,然后对该变量初始化,对该变量实现相应的监听事件,在监听事件里写上

Intent intent = new Intent(MainActivity.this,Second.class);(补充:这里的第一个参数是本Activity.this,第二个参数是要跳转的页面所对应的Activity的.class)Bundle bundle = new Bundle();bundle.putSerializable("user",user);intent.putExtras(bundle);startActivity(intent)

大致就是这样,接下来说第二种方式(putParelable)
创建一个类Book实现parcelable接口

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>() {        @Override        public Book createFromParcel(Parcel source) {            // TODO Auto-generated method stub            Book mBook = new Book();            mBook.bookname=source.readString();            mBook.author=source.readString();            mBook.publishtime=source.readInt();            return mBook;        }        @Override        public Book[] newArray(int size) {            // TODO Auto-generated method stub            return new Book[size];        }    };    @Override    public int describeContents() {        // TODO Auto-generated method stub        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        // TODO Auto-generated method stub        dest.writeString(bookname);        dest.writeString(author);        dest.writeInt(publishtime);    }}

在mianActivity里定义第二个按钮,然后初始化,具体代码如下

package com.example.bundledemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button btn1;    public static final String SER_KEY="com.tutor.objecttran.ser";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn1=(Button) findViewById(R.id.btn1);        btn1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Book book = new Book();                book.setAuthor("ffddhf");                book.setBookname("ertfser");                book.setPublishtime(1555);                Intent intent = new Intent(MainActivity.this,Second.class);                Bundle bundle=new Bundle();                bundle.putParcelable(SER_KEY, book);                //Log.i("fff", book.getAuthor());                intent.putExtras(bundle);                startActivity(intent);            }        });    }}

接着在创建第二个Activity并将其在AndroidManifest.xml里声明
具体代码如下(声明就不多说了)

import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class Second extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        TextView textview = new TextView(this);        Book book = getIntent().getParcelableExtra(MainActivity.SER_KEY);        textview.setText("BOOK name is:"+book.getBookname()+"\n"+"Author is:"+book.getAuthor()+"\n"+"PublishTime is:"+book.getPublishtime());        setContentView(textview);    }}

这样就写好了运行一下,结果如图所示

这里写图片描述

点击按钮后如图显示

这里写图片描述

0 0
原创粉丝点击