安卓对象传递_二
2016-05-30 20:56
323 查看
1.intent没有直接传递对象的,两种方法传递。cat类的比较耗资源,dog推荐使用
public class Cat implements Serializable{
String name;
int age;
String type;
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}
public class Dog implements Parcelable {
String name;
int age;
String type;
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(type);
}
public static final Parcelable.Creator<Dog> CREATOR
= new Parcelable.Creator<Dog>() {
public Dog createFromParcel(Parcel in) {
Dog dog = new Dog();
dog.name = in.readString();
dog.age = in.readInt();
dog.type = in.readString();
return dog;
}
public Dog[] newArray(int size) {
return new Dog[size];
}
};
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}
2.传递发生方法
public void send_cat(View view){
Cat cat = new Cat();
cat.name = "pipi";
cat.age = 18;
cat.type = "mmm";
Intent cat_mass = new Intent(this,Activity_m.class);
cat_mass.putExtra("cat",cat);
startActivity(cat_mass);
}
public void send_dog(View view){
Dog dog = new Dog();
dog.name = "qqq";
dog.age = 123;
dog.type = "dog";
Intent dog_mass = new Intent(this,Activity_m.class);
dog_mass.putExtra("dog",dog);
startActivity(dog_mass);
}
3.事件布局
<Button
android:id = "@+id/send_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:onClick="send_cat"
android:text = "cat"
android:textColor="#00ff00"
/>
<Button
android:id = "@+id/send_dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:onClick="send_dog"
android:text = "dog"
android:textColor="#00ff00"
/>
3.接受事件方法
public class Activity_m extends AppCompatActivity {
private EditText message;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_m);
message = (EditText) findViewById(R.id.text_message);
Intent intent = getIntent();
//Cat cat = (Cat) intent.getSerializableExtra("cat");
Dog dog = intent.getParcelableExtra("dog");
//message.setText(cat.toString());
message.setText(dog.toString());
}
}
public class Cat implements Serializable{
String name;
int age;
String type;
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}
public class Dog implements Parcelable {
String name;
int age;
String type;
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(type);
}
public static final Parcelable.Creator<Dog> CREATOR
= new Parcelable.Creator<Dog>() {
public Dog createFromParcel(Parcel in) {
Dog dog = new Dog();
dog.name = in.readString();
dog.age = in.readInt();
dog.type = in.readString();
return dog;
}
public Dog[] newArray(int size) {
return new Dog[size];
}
};
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}
2.传递发生方法
public void send_cat(View view){
Cat cat = new Cat();
cat.name = "pipi";
cat.age = 18;
cat.type = "mmm";
Intent cat_mass = new Intent(this,Activity_m.class);
cat_mass.putExtra("cat",cat);
startActivity(cat_mass);
}
public void send_dog(View view){
Dog dog = new Dog();
dog.name = "qqq";
dog.age = 123;
dog.type = "dog";
Intent dog_mass = new Intent(this,Activity_m.class);
dog_mass.putExtra("dog",dog);
startActivity(dog_mass);
}
3.事件布局
<Button
android:id = "@+id/send_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:onClick="send_cat"
android:text = "cat"
android:textColor="#00ff00"
/>
<Button
android:id = "@+id/send_dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:onClick="send_dog"
android:text = "dog"
android:textColor="#00ff00"
/>
3.接受事件方法
public class Activity_m extends AppCompatActivity {
private EditText message;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_m);
message = (EditText) findViewById(R.id.text_message);
Intent intent = getIntent();
//Cat cat = (Cat) intent.getSerializableExtra("cat");
Dog dog = intent.getParcelableExtra("dog");
//message.setText(cat.toString());
message.setText(dog.toString());
}
}
相关文章推荐
- 径向基神经网络RBF
- UML之用例图
- jq过滤器方法 选择器
- Easy-题目57:28. Implement strStr()
- request.getParameter() 和request.getAttribute() 区别
- appserv - 最简单的绑定路径
- 用malloc动态申请一个二维数组的三种方法
- 正则大全
- 51nod 棋盘问题(博弈论)
- shell下时间日期的加减乘除运算
- 手动为ubuntu虚拟机配置Ip地址
- 【Java学习笔记】进制转换
- 异常
- Python学习之基础总结--1
- 《深入浅出Node.js》学习笔记——(四)异步编程
- Easy-题目56:303. Range Sum Query – Immutable
- OpenCV学习Laplacian 变换及其运用
- 接口
- hdu-2035-人见人爱A^B-数的快速幂
- 本周任务