方法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();
}
}

Android_Intent_passObject的更多相关文章

  1. python写的爬虫工具,抓取行政村的信息并写入到hbase里

    python的版本是2.7.10,使用了两个第三方模块bs4和happybase,可以通过pip直接安装. 1.logger利用python自带的logging模块配置了一个简单的日志输出 2.get ...

随机推荐

  1. Centos4.3安装MySQL-python-1.2.3,出现error: command 'gcc' failed with exit status 1

    在Linux Centos 4.3上安装MySQL-python-1.2.3的时候出现error: command 'gcc' failed with exit status 1, 具体原因是因为没有 ...

  2. iis 5.1 连接 sql 2005

    VS2005+SQL2005 ASP.NET2.0数据库连接总结 通过上篇文章(http://www.cnblogs.com/user34j/archive/2007/01/23/628426.htm ...

  3. App Store 加急审核方式

    https://developer.apple.com/contact/app-store/?topic=expedite 1:理由一般是用户安全问题或者崩溃问题成功率会高一些. 如果是崩溃问题,你最 ...

  4. ES5新特性:理解 Array 中增强的 9 个 API

    为了更方便的对JS中Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.index ...

  5. linq中的contains条件

    linq中的contains条件   在sql查询语句中,in 在linq 中用contains,并且contains前面是数组,而后面是列名,如: SELECT distinct BH FROM c ...

  6. Fiddler On Linux

    参考链接: http://www.development-cycle.com/2013/08/debugging-web-applications-with-fiddler-on-linux/ htt ...

  7. vs2010的一个opencv插件

    调试时,可视化mat等图像 下载及使用地址: http://visualstudiogallery.msdn.microsoft.com/657956e4-8e02-4764-8022-72a0c9c ...

  8. 察看so文件的依赖关系

    使用arm-linux-androideabi-readelf 察看依赖动态库 /android-ndk-r8d/toolchains/arm-linux-androideabi-4.7/prebui ...

  9. ab的排列 aa , ab ba,bb

    package reverseList; public class Main { static void perm(char c[],int lev,char ans[]) { if(c.length ...

  10. kernel网址

    http://www.kernel.org HTTP https://www.kernel.org/pub/ FTP ftp://ftp.kernel.org/pub/ http://www.oldl ...