• 序列化(二)Parcelable


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

      1.1 build.gradle 中添加引 dataBinding

    1. buildFeatures {
    2. dataBinding = true
    3. }

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

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

    2. 创建实体类 Student.java

    1. public class Student implements Parcelable{
    2. private String name;
    3. private int age;
    4. private Score score;
    5. public Student(String name, int age, Score score) {
    6. this.name = name;
    7. this.age = age;
    8. this.score = score;
    9. }
    10. protected Student(Parcel in) {
    11. name = in.readString();
    12. age = in.readInt();
    13. score = in.readParcelable(Score.class.getClassLoader());
    14. }
    15. @Override
    16. public void writeToParcel(Parcel dest, int flags) {
    17. dest.writeString(name);
    18. dest.writeInt(age);
    19. dest.writeParcelable(score, flags);
    20. }
    21. @Override
    22. public int describeContents() {
    23. return 0;
    24. }
    25. public static final Creator CREATOR = new Creator() {
    26. @Override
    27. public Student createFromParcel(Parcel in) {
    28. return new Student(in);
    29. }
    30. @Override
    31. public Student[] newArray(int size) {
    32. return new Student[size];
    33. }
    34. };
    35. public String getName() {
    36. return name;
    37. }
    38. public int getAge() {
    39. return age;
    40. }
    41. public Score getScore() {
    42. return score;
    43. }
    44. }
    45. class Score implements Parcelable {
    46. private int math;
    47. private int english;
    48. public Score(int math, int english) {
    49. this.math = math;
    50. this.english = english;
    51. }
    52. protected Score(Parcel in) {
    53. math = in.readInt();
    54. english = in.readInt();
    55. }
    56. @Override
    57. public void writeToParcel(Parcel dest, int flags) {
    58. dest.writeInt(math);
    59. dest.writeInt(english);
    60. }
    61. @Override
    62. public int describeContents() {
    63. return 0;
    64. }
    65. public static final Creator CREATOR = new Creator() {
    66. @Override
    67. public Score createFromParcel(Parcel in) {
    68. return new Score(in);
    69. }
    70. @Override
    71. public Score[] newArray(int size) {
    72. return new Score[size];
    73. }
    74. };
    75. public int getMath() {
    76. return math;
    77. }
    78. public int getEnglish() {
    79. return english;
    80. }
    81. }

    3. 测试页面 MainActivity

      3.1 MainActivity.java

    1. public class MainActivity extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    6. binding.button.setOnClickListener(new View.OnClickListener() {
    7. @Override
    8. public void onClick(View view) {
    9. String name = binding.etName.getText().toString().trim();
    10. int age = Integer.parseInt(binding.etAge.getText().toString().trim());
    11. int math = Integer.parseInt(binding.etMath.getText().toString().trim());
    12. int english = Integer.parseInt(binding.etEnglish.getText().toString().trim());
    13. Score score = new Score(math, english);
    14. Student student = new Student(name, age, score);
    15. //MyApplication application = (MyApplication) getApplication();
    16. //application.student = student;
    17. Intent intent = new Intent(MainActivity.this, MainActivity2.class);
    18. Bundle bundle = new Bundle();
    19. bundle.putParcelable("student", student);
    20. intent.putExtra("data", bundle);
    21. startActivity(intent);
    22. }
    23. });
    24. }
    25. }

      3.2 activity_main.xml

    1. "1.0" encoding="utf-8"?>
    2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools">
    5. <data>
    6. data>
    7. <androidx.constraintlayout.widget.ConstraintLayout
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent"
    10. tools:context=".MainActivity">
    11. <androidx.constraintlayout.widget.Guideline
    12. android:id="@+id/guideline2"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:orientation="horizontal"
    16. app:layout_constraintGuide_percent="0.6" />
    17. <EditText
    18. android:id="@+id/et_name"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:ems="10"
    22. android:hint="name"
    23. android:inputType="textPersonName"
    24. app:layout_constraintBottom_toTopOf="@+id/et_age"
    25. app:layout_constraintEnd_toEndOf="@+id/et_age"
    26. app:layout_constraintStart_toStartOf="@+id/et_age"
    27. app:layout_constraintTop_toTopOf="parent" />
    28. <EditText
    29. android:id="@+id/et_age"
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:ems="10"
    33. android:hint="Age"
    34. android:inputType="number"
    35. app:layout_constraintBottom_toTopOf="@+id/et_math"
    36. app:layout_constraintEnd_toEndOf="@+id/et_math"
    37. app:layout_constraintStart_toStartOf="@+id/et_math"
    38. app:layout_constraintTop_toBottomOf="@+id/et_name" />
    39. <EditText
    40. android:id="@+id/et_math"
    41. android:layout_width="wrap_content"
    42. android:layout_height="wrap_content"
    43. android:ems="10"
    44. android:hint="Math"
    45. android:inputType="number"
    46. app:layout_constraintBottom_toTopOf="@+id/et_english"
    47. app:layout_constraintEnd_toEndOf="@+id/et_english"
    48. app:layout_constraintStart_toStartOf="@+id/et_english"
    49. app:layout_constraintTop_toBottomOf="@+id/et_age" />
    50. <EditText
    51. android:id="@+id/et_english"
    52. android:layout_width="wrap_content"
    53. android:layout_height="wrap_content"
    54. android:ems="10"
    55. android:hint="English"
    56. android:inputType="number"
    57. app:layout_constraintBottom_toTopOf="@+id/button"
    58. app:layout_constraintEnd_toEndOf="@+id/button"
    59. app:layout_constraintStart_toStartOf="@+id/button"
    60. app:layout_constraintTop_toBottomOf="@+id/et_math" />
    61. <Button
    62. android:id="@+id/button"
    63. android:layout_width="wrap_content"
    64. android:layout_height="wrap_content"
    65. android:text="Button"
    66. app:layout_constraintBottom_toTopOf="@+id/guideline2"
    67. app:layout_constraintEnd_toEndOf="parent"
    68. app:layout_constraintStart_toStartOf="parent"
    69. app:layout_constraintTop_toBottomOf="@+id/et_english" />
    70. androidx.constraintlayout.widget.ConstraintLayout>
    71. layout>

    4. 测试页面 MainActivity

      4.1 MainActivity2.java

    1. public class MainActivity2 extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. ActivityMain2Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
    6. Intent intent = getIntent();
    7. Bundle bundle = intent.getBundleExtra("data");
    8. Student student = bundle.getParcelable("student");
    9. //MyApplication application = (MyApplication) getApplication();
    10. //Student student = application.student;
    11. binding.tvName.setText(student.getName());
    12. binding.tvAge.setText(String.valueOf(student.getAge()));
    13. binding.tvMath.setText(String.valueOf(student.getScore().getMath()));
    14. binding.tvEnglish.setText(String.valueOf(student.getScore().getEnglish()));
    15. }
    16. }

      4.2 activity_main2.xml

    1. "1.0" encoding="utf-8"?>
    2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools">
    5. <data>
    6. data>
    7. <androidx.constraintlayout.widget.ConstraintLayout
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent"
    10. tools:context=".MainActivity2">
    11. <TextView
    12. android:id="@+id/tv_name"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:text="TextView"
    16. android:textSize="24sp"
    17. app:layout_constraintBottom_toTopOf="@+id/tv_age"
    18. app:layout_constraintEnd_toEndOf="@+id/tv_age"
    19. app:layout_constraintStart_toStartOf="@+id/tv_age"
    20. app:layout_constraintTop_toTopOf="parent" />
    21. <TextView
    22. android:id="@+id/tv_age"
    23. android:layout_width="wrap_content"
    24. android:layout_height="wrap_content"
    25. android:text="TextView"
    26. android:textSize="24sp"
    27. app:layout_constraintBottom_toTopOf="@+id/tv_math"
    28. app:layout_constraintEnd_toEndOf="@+id/tv_math"
    29. app:layout_constraintStart_toStartOf="@+id/tv_math"
    30. app:layout_constraintTop_toBottomOf="@+id/tv_name" />
    31. <TextView
    32. android:id="@+id/tv_math"
    33. android:layout_width="wrap_content"
    34. android:layout_height="wrap_content"
    35. android:text="TextView"
    36. android:textSize="24sp"
    37. app:layout_constraintBottom_toTopOf="@+id/tv_english"
    38. app:layout_constraintEnd_toEndOf="@+id/tv_english"
    39. app:layout_constraintStart_toStartOf="@+id/tv_english"
    40. app:layout_constraintTop_toBottomOf="@+id/tv_age" />
    41. <TextView
    42. android:id="@+id/tv_english"
    43. android:layout_width="wrap_content"
    44. android:layout_height="wrap_content"
    45. android:text="TextView"
    46. android:textSize="24sp"
    47. app:layout_constraintBottom_toTopOf="@+id/guideline3"
    48. app:layout_constraintEnd_toEndOf="parent"
    49. app:layout_constraintStart_toStartOf="parent"
    50. app:layout_constraintTop_toBottomOf="@+id/tv_math" />
    51. <androidx.constraintlayout.widget.Guideline
    52. android:id="@+id/guideline3"
    53. android:layout_width="wrap_content"
    54. android:layout_height="wrap_content"
    55. android:orientation="horizontal"
    56. app:layout_constraintGuide_percent="0.63" />
    57. androidx.constraintlayout.widget.ConstraintLayout>
    58. layout>

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

    1. public class MyApplication extends Application {
    2. Student student;
    3. }

    6. 效果图

         

     

     

  • 相关阅读:
    多线程与高并发——并发编程(5)
    python中的NumPy和Pandas往往都是同时使用,NumPy和Pandas的在数据分析中的联合使用
    FPGA信号处理系列文章——码元同步
    JAVA ---List
    自适应模糊神经网络与bp神经网络的区别
    面试逻辑题,有8个小球和一个天平,一个小球偏重,其它小球一样重,问最少称几次可以找出那个重球?
    证书-解决非对称加密的公钥信任问题
    SAP-PP-查询报工数据
    AutoCAD Electrical 2022——创建项目
    异构数据源同步之数据同步 → datax 再改造,开始触及源码
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126888743