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
- Intent传递对象的两种方式(putSerializable,putParelable)
- Android中Intent传递对象的两种方法(Bundle.putParcelable/putSerializable)!
- (4.1.30.1)Android中Intent传递对象的两种方法(Bundle.putParcelable/putSerializable)!
- Android中Intent传递对象的两种方法(Bundle.putParcelable/putSerializable)!
- Intent传递对象的两种方式
- intent传递对象的两种方式
- 使用Intent传递对象的两种方式
- 使用Intent传递对象的两种方式
- Android使用Intent传递对象的两种方式
- intent传递对象的两种方法
- intent传递对象的两种方法
- intent传递对象的两种方法
- Intent传递对象的两种实现
- Intent传递对象的两种方法
- Intent 传递对象的两种方法
- Intent传递对象的几种方式
- Intent传递对象的几种方式
- Intent传递值对象的两种方式:Serializable和Parcelable
- Mysql创建数据库为中文字符
- 15.3 List接口特有方法
- ACL 过滤
- MacBookPro打造开发环境
- Laravel系列之windows环境安装配置(一)
- Intent传递对象的两种方式(putSerializable,putParelable)
- Linux下基于http的小型web服务器编写
- 欢迎使用CSDN-markdown编辑器
- JAVA中集合框架概述
- 【mybatis】IF判断的坑
- LINUX常用命令大全
- 笔记本安装WindowsXP系统时蓝屏了怎么办
- <AlertDialog>的简单用法与功能
- iOS多线程编程--NSOperation