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 ...
随机推荐
- 设置将 Microsoft Azure 的网络基础结构以支持设置为灾难恢复站点
Prateek Sharma 云 + Enterprise 高级项目经理 Azure SiteRecovery (ASR)可以将Microsoft Azure用作您的虚拟机的灾难恢复站点. 当管理 ...
- 虚拟主机 (Virtual Host)
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...
- 利用URLRewriter重写url地址
首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDN ...
- tcxtreelist 控制单元格变颜色,或者行变颜色
如果控制单元格变颜色,只需要把注释的放开就可以了, 也就是判断当前列,是否是你想让变颜色的列. 如果想整行变颜色, 则只需要注释下面的就可以了. procedure TfrmwpOrderSendin ...
- HDU-4515 小Q系列故事——世界上最遥远的距离
小Q系列故事——世界上最遥远的距离 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) ...
- 为EF DbContext生成的实体添加注释(T5模板应用)[转]
1 先加上类注释 找到这行代码WriteHeader(codeStringGenerator, fileManager): 在它下面加上我们的代码: string summary=string.Emp ...
- C# 多线程是否结束可通过线程池可以判断
C# ManualResetEvent信号状态判断线程池是否结束 这是一段重要的代码,小猪两个小时的研究成果,记下来备查. using System; using System.Collection ...
- curl post请求
libcurl发送post请求,包括httpheader参数 static size_t getCharCode(void *ptr, size_t size, size_t nmemb, void ...
- 泰泽新闻:英特尔三星双否认泰泽Tizen系统已死
7月8日 据媒体TizenExperts报道,关于“Tizen系统跳票”的传闻已经遭到了英特尔和三星否认. 此前传闻三星自行研制的智能手机Tizen操作系统流产,但如今已经遭到了官方的否认. 英特尔三 ...
- zabbix 获取不到自定义脚本的值解决
agent端: zabbix 自定义脚本 [root@localhost script]# cat check_ping.sh #!/bin/bash result=$(/usr/local/nagi ...