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. 效果图
? ? ?
?
?
|