zoukankan      html  css  js  c++  java
  • Parcelable实现数据组件的数据传递

    FirstActivity.java

    package com.sdutacm.getcontextskillpractise;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class FirstActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Person person = new Person();
            person.setAge(18);
            person.setName("Tom");
            Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
            intent.putExtra("person_data",person);
            startActivity(intent);
        }
    }
    

    SecondActivity.java

    package com.sdutacm.getcontextskillpractise;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    /**
     * Created by bummer on 2017/8/9.
     */
    
    public class SecondActivity extends AppCompatActivity{
       public static String TAG = "SecondActivity";
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Person person = (Person) getIntent().getParcelableExtra("person_data");
            Log.d(TAG,"person name is "+person.getName());
            Log.d(TAG,"person age is "+person.getAge());
    
        }
    }
    

    Person.java

    package com.sdutacm.getcontextskillpractise;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    /**
     * Created by bummer on 2017/8/9.
     */
    
    public class Person implements Parcelable {
       private String name;
        private int age;
    
        public static Creator<Person> getCREATOR() {
            return CREATOR;
        }
    
        protected Person(Parcel in) {
            name = in.readString();
            age = in.readInt();
        }
    
        public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
            @Override
            public Person createFromParcel(Parcel in) {
                Person person = new Person();
                person.name = in.readString(); //读取name
                person.age = in.readInt(); //读取年龄
                return person;
            }
    
            @Override
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Person() {
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);  //写出name
            dest.writeInt(age);   //写出age
        }
    }
    
  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781850.html
Copyright © 2011-2022 走看看