import android.os.Parcel;
import android.os.Parcelable;
public class UserInfo implements Parcelable {
private String username;
private String password;
public UserInfo(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
//实现Parcel接口必须覆盖实现的方法
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
/*
* 将UserInfo对象的成员写入Parcel
* 注:Parcel中的数据是按顺序写入和读取的,即先被写入的就会先被读取出来
*/
dest.writeString(username);
dest.writeString(password);
}
//该静态域是必须要有的,而且名字必须是CREATOR,否则会出错
public static final Parcelable.Creator<UserInfo> CREATOR =
new Parcelable.Creator<UserInfo>() {
@Override
public UserInfo createFromParcel(Parcel source) {
// TODO Auto-generated method stub
String username = source.readString();
String password = source.readString();
return new UserInfo(username, password);
}
@Override
public UserInfo[] newArray(int size) {
// TODO Auto-generated method stub
return new UserInfo[size];
}
};
}
Parcelable接口的实现
最新推荐文章于 2022-02-24 21:58:23 发布