Android_Intent_passObject
方法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的更多相关文章
- python写的爬虫工具,抓取行政村的信息并写入到hbase里
python的版本是2.7.10,使用了两个第三方模块bs4和happybase,可以通过pip直接安装. 1.logger利用python自带的logging模块配置了一个简单的日志输出 2.get ...
随机推荐
- Memcached‘process_bin_delete’函数安全漏洞
漏洞名称: Memcached‘process_bin_delete’函数安全漏洞 CNNVD编号: CNNVD-201401-174 发布时间: 2014-01-15 更新时间: 2014-01-1 ...
- 使用Win32/ATL建立窗口的过程
有时候想写个几十kb的小程序,MFC实在是太大了,Win32有时又太麻烦,怎么办呢?用ATL写会更方便和轻量级一些 ATL和MFC不同的是,ATL并没有强制性封装WinMain函数,仅封装了WndPr ...
- angular.extend
function f1() {} var f2 = angular.extend(f1, { active: false, toggle: function() { this.active = !th ...
- DataGrid能否动态合并一笔订单下面的多个交易
/** * author ____′↘夏悸 * create date 2012-11-5 **/ $.extend($.fn.datagrid.methods, { autoMergeCells : ...
- python __enter__ 与 __exit__的作用,以及与 with 语句的关系
转载自:http://linbo.github.io/2013/01/08/python-with/ (一直不知道博客园哪里发转载文章) With语句是什么? 有一些任务,可能事先需要设置,事后做清理 ...
- Html笔记(二)字体
字体标签:<font> 例:<font size=5 color=red>字体标签示例</font> 注:简单颜色可以直接写对应的英文,复杂的颜色用16进制表示,表 ...
- linux 小技巧总结
(1)linux判断文件是否存在 if [ -f filename]: then ......#要执行的语句 fi 具体例子: file=/usr/local/oracle/oradata ...
- 不同的jar里边相同的包名类名怎么区别导入
今天在做项目的时候遇到了一个很有意思的问题,折磨了我很长时间,不过最终还是解决了,特留此文纪念一下. 遇到的问题: 同样一段代码,在同事那就好使,在我这就找不到一个方法.引用的包也都是相同的,这种问题 ...
- HDOJ-ACM1071(JAVA) 定积分
这道题做起来有点无奈,定积分已经忘得差不多了~还可恶的去搜索了抛物线的解析式的求法~哈哈 不过求出来的结果不对...等有时间再去研究这个数学问题吧 - - 以下是JAVA实现: import java ...
- POJ1873 - Balance(01背包)
题目大意 现有一个天平,它有C个挂钩和G个砝码,问有多少种方法可以使得天平平衡(砝码必须用完) 题解 其实就是让背包容量为0的方法有多少种方法,但是这样的话背包容量会出现负数,所以可以平移一下,背包容 ...