zoukankan      html  css  js  c++  java
  • Android高级技巧-intent传递对象

    一、使用intent传递对象

    (1)使用serializable接口

    persion类

    package com.example.jixujinjie;
    
    import java.io.Serializable;
    
    /**
     * Created by 海绵宝宝 on 2019/4/25.
     */
    
    public class Persion  implements Serializable{//intent不允许传递对象类所以我们把他序列化,继承Serializab
        private int age;
        private String name;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }

    MainActivity中使用Intent传值

    public void onClick(View v) {
                    Persion persion=new Persion();
                    persion.setAge(18);
                    persion.setName("王怀宇");
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    intent.putExtra("persion_mes",persion);
                    startActivity(intent);
                }

    Mian2Activity中接受

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Persion persion=(Persion)getIntent().getSerializableExtra("persion_mes");
            TextView textView1=(TextView)findViewById(R.id.T1);
            TextView textView2=(TextView)findViewById(R.id.T2);
            textView1.setText("姓名:"+persion.getName());
            textView2.setText("年龄:"+persion.getAge());
    
        }

    (2)使用parcelable方式

    persion类

    package com.example.jixujinjie;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    import java.io.Serializable;
    
    /**
     * Created by 海绵宝宝 on 2019/4/25.
     */
    
    public class Persion  implements Parcelable{//继承Parcelable
        private int age;
        private String name;
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        //writeToParcel和describeContents是继承Parcelabel的必须重写的函数
        @Override
        public int describeContents() {
            return 0;
        }
      //在writeToParcel将类的属性写入
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeInt(age);
        }
        //创建一个Creator接口的实现,createFromParcel中将之前写入的取出,注意顺序和之前一致
        public static final Parcelable.Creator<Persion> CREATOR=new Parcelable.Creator<Persion>(){
            @Override
            public Persion createFromParcel(Parcel source) {
                Persion persion=new Persion();
                persion.name=source.readString();
                persion.age=source.readInt();
                return  persion;
            }
    
            @Override
            public Persion[] newArray(int size) {
                return new Persion[size];
            }
        };
    
    }

    Main2Activity.java

    package com.example.jixujinjie;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    //获取方式换成getParcelableExtra
    Persion persion=(Persion)getIntent().getParcelableExtra("persion_mes");
    TextView textView1=(TextView)findViewById(R.id.T1);
    TextView textView2=(TextView)findViewById(R.id.T2);
    textView1.setText("姓名:"+persion.getName());
    textView2.setText("年龄:"+persion.getAge());

    }
    }
  • 相关阅读:
    C语言的swap函数的易错点
    C语言结构体指针,及其错误示范
    操作系统--问答题
    数据库-存储过程、触发器、视图-考研笔记
    数据库知识-恢复子系统、数据库恢复的基本技术、转储方法、日志文件、undo/redo操作、数据库镜像
    c语言,快排序找出第k小/大的数
    STM32的Flash读写保护,SWD引脚锁的各种解决办法汇总(2020-03-10)
    uCOS全家桶PDF文档整理汇总贴,提供论坛,百度云和腾讯云下载(2020-03-09)
    分享下之前做的STM32嵌入式Web完整设计教程和案例html,Ajax,Javacript,XML,cgi等
    【STM32H7教程】第71章 STM32H7的内部Flash应用之模拟EEPROM
  • 原文地址:https://www.cnblogs.com/837634902why/p/10771871.html
Copyright © 2011-2022 走看看