方法4. 把基本的数据类型封装到一个对象中,然后通过intent传递该对象
需要考虑对Person对象进行序列化

MainActivity:

package com.example.day06_activity4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void btn_click(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
// 创建一个Person对象,并通过intent传递到OtherActivity
Person person = new Person("志明", 40, 333.3);
intent.putExtra("person", person);
startActivity(intent);
}
}

OtherActivity:

package com.example.day06_activity4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView; public class OtherActivity extends Activity {
private TextView textview; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
textview = (TextView) findViewById(R.id.text);
// 获得intent
Intent intent = getIntent();
Person person = intent.getParcelableExtra("person");
String name = person.getName();
int age = person.getAge();
double weight = person.getWeight(); textview.setText("姓名:" + name + ", 年龄:" + age + ", 体重:" + weight);
} }

Person

package com.example.day06_activity4;

import android.os.Parcel;
import android.os.Parcelable; public class Person implements Parcelable { private String name;
private int age;
private double weight; public Person() {
} public Person(String name, int age, double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
} // getXxx()方法和setXxx()方法
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;
} public double getWeight() {
return weight;
} public void setWeight(double weight) {
this.weight = weight;
} // 实现Parcelable接口中的抽象方法
// describeContents()方法基本上直接返回0,就可以了
public int describeContents() {
return 0;
}
// 把需要序列化的参数写入out中
public void writeToParcel(Parcel out, int flags) {
out.writeString(name);
out.writeInt(age);
out.writeDouble(weight);
}
// 定义一个静态的属性CREATOR 是Parcelable.Creator的对象
// 在该匿名内部类中,实现Parcelable.Creator中的两个方法(泛型参数为类名)
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
// createFromParcel(Parcel in)
// 对in进行反序列化(需要通过Person的构造器实现)
public Person createFromParcel(Parcel in) {
return new Person(in);
}
// newArray(Parcel in)
// 反序列化多个元素时使用
public Person[] newArray(int size) {
return new Person[size];
}
};
// 定义私有的构造器 从in中反序列对应的参数(反序列化参数的顺序必须与序列化参数的顺序保持一致)
private Person(Parcel in) {
name = in.readString();
age = in.readInt();
weight = in.readDouble();
}
}

最新文章

  1. Qt——浅谈样式表
  2. Tomcat中JVM内存溢出及合理配置及maxThreads如何配置(转)
  3. Android:Activity生命周期
  4. PPTP-VPN第三章——用户流量与并发数限制
  5. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
  6. android-tools-adb
  7. union的用法
  8. Jsop入门程序
  9. mysq 日期相减
  10. FZU 1893 内存管理 模拟
  11. 迅雷最新bug已被找到!mac不用登录也可以极速下载了!
  12. AGC016D - XOR Replace 置换/轮换
  13. Shiro核心概述
  14. 在Unity5.6.5f1中使用C#7语法
  15. 购物车的实现(jsp的session+Java的Map的结合)
  16. UVa 122 Trees on the level(二叉树层序遍历)
  17. Python FTP文件传输
  18. 【转】jquery cookie操作
  19. mini2440:通过JLink烧写BootLoader到Nor Flash
  20. [转]Hspice和Spice Explorer许可文件设置时环境变量FLEXLM_BATCH = 1的一些现象

热门文章

  1. hdu4681String
  2. hadoop 序列化源码浅析
  3. easyui datagriad 框架 自适应r
  4. ASP.NET操作Word的IIS权限配置
  5. SQL Server 地理数据库中的系统表
  6. import project后,出现Unable to get system library for the project
  7. Set up your first C# test with NUnit or resharper
  8. Java&amp;MySQL Type Mapping
  9. BP神经网络模型及算法推导
  10. ios iphone 将log在终端输出