如何使用 Bundle 传递数据
展开全部
Book类:
package com.bundletest.model.fneg;
import java.io.Serializable;
/**
*@Copyright:Copyright (c) 2008 - 2100
*@Company:Sagret
*@Author:fengcunhan fengcunhan@gmail.com
*@Package:com.bundletest.model.fneg
*@FileName:Book.java
*@Time:2010-12-19
*@User:feng
*/
public class Book implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String id;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
实例化Book类,得到Book对象book以及设置成员变量:
if(TextUtils.isEmpty(bookName)||TextUtils.isEmpty(author)||TextUtils.isEmpty(id)){
Toast.makeText(AndroidBundleActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show();
}else{
Book book=new Book();
book.setName(bookName);
book.setAuthor(author);
book.setId(id);
Intent intent=new Intent(AndroidBundleActivity.this,RecieveActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("book", book);
intent.putExtras(bundle);
startActivity(intent);
}
在另一个Activity中获取传递过来的book对象,并显示:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
Book book=(Book)bundle.getSerializable("book");
nameText.setText("书名:"+book.getName());
authorText.setText("作者:"+book.getAuthor());
idText.setText("ID:"+book.getId());
package com.bundletest.model.fneg;
import java.io.Serializable;
/**
*@Copyright:Copyright (c) 2008 - 2100
*@Company:Sagret
*@Author:fengcunhan fengcunhan@gmail.com
*@Package:com.bundletest.model.fneg
*@FileName:Book.java
*@Time:2010-12-19
*@User:feng
*/
public class Book implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String id;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
实例化Book类,得到Book对象book以及设置成员变量:
if(TextUtils.isEmpty(bookName)||TextUtils.isEmpty(author)||TextUtils.isEmpty(id)){
Toast.makeText(AndroidBundleActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show();
}else{
Book book=new Book();
book.setName(bookName);
book.setAuthor(author);
book.setId(id);
Intent intent=new Intent(AndroidBundleActivity.this,RecieveActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("book", book);
intent.putExtras(bundle);
startActivity(intent);
}
在另一个Activity中获取传递过来的book对象,并显示:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
Book book=(Book)bundle.getSerializable("book");
nameText.setText("书名:"+book.getName());
authorText.setText("作者:"+book.getAuthor());
idText.setText("ID:"+book.getId());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询