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 ...
随机推荐
- 函数lock_mode_stronger_or_eq 锁权限等级
row代表lock HashTable的权限 column代表预加锁的权限 ulint lock_mode_stronger_or_eq( /*=====================*/ e ...
- wps 2012-2013 通杀漏洞(CVE-2013-3934)
测试方法: 本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! #!/usr/bin/python # Exploit Title: Kingsoft Office Writer ...
- 转自作者:phylips@bmy
差分约束系统 2008-11-28 20:53:25| 分类: 算法与acm|举报|字号 订阅 出处:http://duanple.blog.163.com/blog/static/7097 ...
- VM8下安装Mac OS X 10.7
下载Mac OS X 10.7 安装包http://115.com/file/clj1iu8m# 下载HJMac http://115.com/file/cljyu1rh# ...
- ☀【移动】UC极速模式
UC浏览器的部分版本默认是“极速”模式,有何办法能控制UC自动改变其浏览模式? √http://www.zhihu.com/question/20582949 关于UC极速模式下访问网站错乱 √htt ...
- 用JavaScript(js)对时间格式化
Date.prototype.format =function(format) { var o = { "M+" : (this.getMo ...
- Apache搭建多个站点
如何用Apache搭建的网站系统上运行多个站点呢?最平常的大概有3种方法. 第一种:单IP不同端口 第二种:多IP同端口(独立IP的虚拟空间) 第三种:域名绑定根目录的方式(共享IP的虚拟空间) 下面 ...
- 第K顺序统计量
1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...
- [Tommas] UNION 和 UNION ALL 的区别
UNION指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看,UNION跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料.UNION的一个限制是两个 SQL 语句所产生的 ...
- java 小结1(static ,final,泛型)
static,final. (1)final: final:属于“终态”,意思就是不可以改变.可以修饰非抽象类,非抽象类的方法等.反正就是不能够再改变赋值了. 注意:1)fina类不能被继承,所以它没 ...