//主界面布局只有一个button,用来跳转到另外一个activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/sendobj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sendobj" />
</RelativeLayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//接收复杂数据类型的activity的布局文件,这里主要是接收一个Bitmap对象
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgview"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/hello_world"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgview"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16dp" />
</RelativeLayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//创建一个bean类
package com.example.passobjdemo;
import android.graphics.Bitmap;
public class ObjBean {
private int id;
private String name;
private double price;
private Bitmap bitmap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public String toString() {
return "id=" + id + ", name=" + name + ", price=" + price;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package com.example.passobjdemo;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
/*
* Android传递复杂类型,Activity之间传递数据是通过Intent实现的。
* Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。
* Parcelable是Android API特有的接口,效率比Serializable高,推荐用这种方法提高性能。
* Parcelable接口的作用:实现了Parcelable接口的实例可以将自身的状态信息写入Parcel,
* 也可以从Parcel中恢复其状态。 Parcel用来完成数据的序列化传递。
* */
public class ParcelableUtil implements Parcelable {
private ObjBean objBean;
public ObjBean getObjBean() {
return objBean;
}
public ParcelableUtil(ObjBean objBean) {
super();
this.objBean = objBean;
}
private ParcelableUtil(Parcel in) {
objBean = new ObjBean();
objBean.setId(in.readInt());
objBean.setName(in.readString());
objBean.setPrice(in.readDouble());
objBean.setBitmap((Bitmap) in.readParcelable(Bitmap.class.getClassLoader()));
}
@Override
public int describeContents() {
return 0;
}
/*
* 对象序列化在这里实现,
* 对每一个属性进行封装,写到流中
* 注意:在其他地方读或者设置值的时候,要跟这里写的顺序保持一致,否则读到的值可能为null
* */
@Override
public void writeToParcel(Parcel in, int flags) {
in.writeInt(objBean.getId());
in.writeString(objBean.getName());
in.writeDouble(objBean.getPrice());
in.writeParcelable(objBean.getBitmap(), PARCELABLE_WRITE_RETURN_VALUE);
}
/*
* API解释: Interface that must be implemented and provided as a public CREATOR field
* that generates instances of your Parcelable class from a Parcel.
*
* 实际上用来对象的反序列化
* */
public static final Parcelable.Creator<ParcelableUtil> CREATOR =
new Parcelable.Creator<ParcelableUtil>() {
@Override
public ParcelableUtil createFromParcel(Parcel source) {
return new ParcelableUtil(source);
}
@Override
public ParcelableUtil[] newArray(int size) {
return new ParcelableUtil[size];
}
};
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.example.passobjdemo;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button sendobj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendobj = (Button) findViewById(R.id.sendobj);
sendobj.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.sendobj) {
ObjBean bean = new ObjBean();
bean.setId(1);
bean.setName("Android smart Phone");
bean.setPrice(999.99);
InputStream is = getResources().openRawResource(R.raw.amz);
Bitmap bit = BitmapFactory.decodeStream(is);
bean.setBitmap(bit);
ParcelableUtil pUtil = new ParcelableUtil(bean);
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("objbean", pUtil);//将ParcelableUtil对象put到intent中
startActivity(intent);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package com.example.passobjdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class AnotherActivity extends Activity {
private ImageView imgView;
private TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
imgView = (ImageView) findViewById(R.id.imgview);
txtView = (TextView) findViewById(R.id.txtview);
Intent intent = getIntent();
//获取从mainactivity传过来的ParcelableUtil对象
ParcelableUtil pu = (ParcelableUtil) intent.getParcelableExtra("objbean");
ObjBean bean = pu.getObjBean();
//显示值
imgView.setImageBitmap(bean.getBitmap());
txtView.setText(bean.getName() + "\n" + bean.getPrice());
}
}
|
android 用Parcelable传递复杂数据类型,布布扣,bubuko.com
android 用Parcelable传递复杂数据类型