序列化(二)Parcelable

1. android 库中的类,执行速度快,使用在进程,Activity 之间对象的传递

  1.1 build.gradle 中添加引 dataBinding

   buildFeatures {
        dataBinding = true
    }

  1.2 AndroidManifest.xml 文件添加配置测试

     //android:process=":process2" 表示MainActivity2在一个新的进程中执行
     <activity
            android:name=".MainActivity2"
            android:process=":process2"
            android:exported="false" />

2. 创建实体类 Student.java

public class Student implements Parcelable{
    private String name;
    private int age;
    private Score score;

    public Student(String name, int age, Score score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    protected Student(Parcel in) {
        name = in.readString();
        age = in.readInt();
        score = in.readParcelable(Score.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeParcelable(score, flags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Score getScore() {
        return score;
    }
}

class Score implements Parcelable {
    private int math;
    private int english;

    public Score(int math, int english) {
        this.math = math;
        this.english = english;
    }

    protected Score(Parcel in) {
        math = in.readInt();
        english = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(math);
        dest.writeInt(english);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Score> CREATOR = new Creator<Score>() {
        @Override
        public Score createFromParcel(Parcel in) {
            return new Score(in);
        }

        @Override
        public Score[] newArray(int size) {
            return new Score[size];
        }
    };

    public int getMath() {
        return math;
    }

    public int getEnglish() {
        return english;
    }
}

3. 测试页面 MainActivity

  3.1 MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = binding.etName.getText().toString().trim();
                int age = Integer.parseInt(binding.etAge.getText().toString().trim());
                int math = Integer.parseInt(binding.etMath.getText().toString().trim());
                int english = Integer.parseInt(binding.etEnglish.getText().toString().trim());
                Score score = new Score(math, english);
                Student student = new Student(name, age, score);
                //MyApplication application = (MyApplication) getApplication();
                //application.student = student;
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("student", student);
                intent.putExtra("data", bundle);
                startActivity(intent);
            }
        });
    }
}

  3.2 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.6" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="name"
            android:inputType="textPersonName"
            app:layout_constraintBottom_toTopOf="@+id/et_age"
            app:layout_constraintEnd_toEndOf="@+id/et_age"
            app:layout_constraintStart_toStartOf="@+id/et_age"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Age"
            android:inputType="number"
            app:layout_constraintBottom_toTopOf="@+id/et_math"
            app:layout_constraintEnd_toEndOf="@+id/et_math"
            app:layout_constraintStart_toStartOf="@+id/et_math"
            app:layout_constraintTop_toBottomOf="@+id/et_name" />

        <EditText
            android:id="@+id/et_math"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Math"
            android:inputType="number"
            app:layout_constraintBottom_toTopOf="@+id/et_english"
            app:layout_constraintEnd_toEndOf="@+id/et_english"
            app:layout_constraintStart_toStartOf="@+id/et_english"
            app:layout_constraintTop_toBottomOf="@+id/et_age" />

        <EditText
            android:id="@+id/et_english"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="English"
            android:inputType="number"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintEnd_toEndOf="@+id/button"
            app:layout_constraintStart_toStartOf="@+id/button"
            app:layout_constraintTop_toBottomOf="@+id/et_math" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintBottom_toTopOf="@+id/guideline2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_english" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

4. 测试页面 MainActivity

  4.1 MainActivity2.java

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMain2Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("data");
        Student student = bundle.getParcelable("student");
        //MyApplication application = (MyApplication) getApplication();
        //Student student = application.student;
        binding.tvName.setText(student.getName());
        binding.tvAge.setText(String.valueOf(student.getAge()));
        binding.tvMath.setText(String.valueOf(student.getScore().getMath()));
        binding.tvEnglish.setText(String.valueOf(student.getScore().getEnglish()));
    }
}

  4.2 activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity2">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintBottom_toTopOf="@+id/tv_age"
            app:layout_constraintEnd_toEndOf="@+id/tv_age"
            app:layout_constraintStart_toStartOf="@+id/tv_age"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintBottom_toTopOf="@+id/tv_math"
            app:layout_constraintEnd_toEndOf="@+id/tv_math"
            app:layout_constraintStart_toStartOf="@+id/tv_math"
            app:layout_constraintTop_toBottomOf="@+id/tv_name" />

        <TextView
            android:id="@+id/tv_math"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintBottom_toTopOf="@+id/tv_english"
            app:layout_constraintEnd_toEndOf="@+id/tv_english"
            app:layout_constraintStart_toStartOf="@+id/tv_english"
            app:layout_constraintTop_toBottomOf="@+id/tv_age" />

        <TextView
            android:id="@+id/tv_english"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_math" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.63" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

5. 创建 MyApplication.java 测试存放公共常量

public class MyApplication extends Application {
    Student student;
}

6. 效果图

     

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hanyang Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值