关闭

安卓对象传递_二

76人阅读 评论(0) 收藏 举报
分类:

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());
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类