最近学习了android的界面的跳转,一个是activity和fragment之间的跳转(之前的博客说过),一个是不同activity之间的跳转。
activity直接的跳转很简单,只需要用Intent即可,而且传递数据很简便,如果只是传递简单数据,即可直接使用,如果传递复杂的数据,比如一个类,就得需要把类定义为。
个人觉得android跳转页面要比JAVA的简单的很多!!。看例子。
下图是从一个Activity跳转到另一个activity,并将数据传递过去。
实体类:(因为我们要传递一个实体类,所有要用到Parcelable)
Android中的Activity传递数据时,为了方便往往将很多数据封装成对象,然后将整个对象传递过去。传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口。
先将Grade implements Parcelable,然后点击Grade,会出现一个红色的小灯泡,点击即可,生成的函数不需要动,位置也不要动,否则会保出错。
package com.example.testparcelable; import android.os.Parcel; import android.os.Parcelable; public class Student implements Parcelable{ private String name; private int age; private Grade grade; public Student(String name, int age, Grade grade) { this.name = name; this.age = age; this.grade = grade; } protected Student(Parcel in) { name = in.readString(); age = in.readInt(); grade = in.readParcelable(Grade.class.getClassLoader()); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); dest.writeParcelable(grade, 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 void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } } class Grade implements Parcelable { private int math; private int chinese; private int English; public Grade(int math, int chinese, int english) { this.math = math; this.chinese = chinese; English = english; } protected Grade(Parcel in) { math = in.readInt(); chinese = in.readInt(); English = in.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(math); dest.writeInt(chinese); dest.writeInt(English); } @Override public int describeContents() { return 0; } public static final Creator<Grade> CREATOR = new Creator<Grade>() { @Override public Grade createFromParcel(Parcel in) { return new Grade(in); } @Override public Grade[] newArray(int size) { return new Grade[size]; } }; public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getChinese() { return chinese; } public void setChinese(int chinese) { this.chinese = chinese; } public int getEnglish() { return English; } public void setEnglish(int english) { English = english; } }
MainActivity:为了操作方便我们使用dataBinding,在里android{defaultConfig{}}里设置,然后在activity_main.xml点击text,将鼠标移到该位置,点击小灯泡,如图。转换为data binding layout。这样我们就不用通过获取ID来对控件进行操作。
package com.example.testparcelable; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.content.Intent; import android.os.Bundle; import android.view.View; import com.example.testparcelable.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = binding.editTextName.getText().toString(); int age = Integer.valueOf(binding.editTextAge.getText().toString()); int chinese = Integer.valueOf(binding.editTextChinese.getText().toString()); int english = Integer.valueOf(binding.editTextEnglish.getText().toString()); int math = Integer.valueOf(binding.editTextMath.getText().toString()); Student student = new Student(name,age,new Grade(math,chinese,english) ); Intent intent = new Intent(MainActivity.this,Main2Activity.class); Bundle bundle = new Bundle(); bundle.putParcelable("student",student); intent.putExtra("bundle", bundle); startActivity(intent); } }); } }
main2Activity:对一做同样的操作。
package com.example.testparcelable; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import com.example.testparcelable.databinding.ActivityMain2Binding; import com.example.testparcelable.databinding.ActivityMainBinding; import org.w3c.dom.Text; public class Main2Activity 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("bundle"); Student student = bundle.getParcelable("student"); binding.textViewName.setText(student.getName()); binding.textViewAge.setText(String.valueOf(student.getAge())); binding.textViewChinese.setText(String.valueOf(student.getGrade().getChinese())); binding.textViewEnglish.setText(String.valueOf(student.getGrade().getEnglish())); binding.textViewMath.setText(String.valueOf(student.getGrade().getMath())); } }