zoukankan      html  css  js  c++  java
  • Intent使用Parcelable传递对象

    package com.pingyijinren.test;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    import java.io.Serializable;
    
    /**
     * Created by Administrator on 2016/5/20 0020.
     */
    public class Person implements Parcelable{
        private String name;
        private int age;
    
        @Override
        public int describeContents(){
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeString(name);
            dest.writeInt(age);
        }
    
        public static final Parcelable.Creator<Person> CREATOR=new Parcelable.Creator<Person>(){
            @Override
            public Person createFromParcel(Parcel source){
                Person person=new Person();
                person.name=source.readString();
                person.age=source.readInt();
                return person;
            }
    
            @Override
            public Person[] newArray(int size){
                return new Person[size];
            }
        };
    
        public void setName(String name){
            this.name=name;
        }
    
        public void setAge(int age){
            this.age=age;
        }
    
        public String getName(){
            return name;
        }
    
        public int getAge(){
            return age;
        }
    }
    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class IndexActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_index);
    
            Intent intent=new Intent(this,TransferObjectActivity.class);
            Person person=new Person();
            person.setAge(26);
            person.setName("张钦雄");
            intent.putExtra("person",person);
            startActivity(intent);
        }
    }
    package com.pingyijinren.test;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class TransferObjectActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_transfer_object);
    
            Person person=getIntent().getParcelableExtra("person");
            Log.d("MainActivity",person.getName()+" "+person.getAge());
        }
    }
  • 相关阅读:
    自定义布局模板
    单据状态图
    初识MongoDB(八)
    初识MongoDB(七)
    初识MongoDB(六)
    初识MongoDB(五)
    初识MongoDB(四)
    初识MongoDB(三)
    初识MongoDB(二)
    初识MongoDB(一)
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5511613.html
Copyright © 2011-2022 走看看