47. Intent
47.1 Intent基本类型
新建工程
更改下名字
new Activity2
Activity1布局
新建一个跳转按钮
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".simple01.MainActivity1">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="startAction"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.dingjiaxiong.myintent.R;
public class MainActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
public void startAction(View view) {
Intent intent = new Intent(this,MainActivity2.class);
intent.putExtra("name","dingjiaxiong");
intent.putExtra("sex","M");
startActivity(intent);
}
}
2
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.dingjiaxiong.myintent.R;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收
Intent intent = getIntent();
String name = intent.getStringExtra("name");
char sex = intent.getCharExtra("sex",'A');
Toast.makeText(this,"name:" + name + "sex:" + sex,Toast.LENGTH_SHORT).show();
}
}
运行
47.2 Intent传递Bundle
1
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.dingjiaxiong.myintent.R;
public class MainActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
public void startAction(View view) {
Intent intent = new Intent(this,MainActivity2.class);
//封装Bundle对象
Bundle bundle = new Bundle();
bundle.putString("name","dingjiaxiong");
bundle.putChar("sex",'M');
//通过intent
intent.putExtras(bundle);
startActivity(intent);
}
}
2
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.dingjiaxiong.myintent.R;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收,拆Bundle
Intent intent = getIntent();
String name = intent.getStringExtra("name");
char sex = intent.getCharExtra("sex",'A');
Toast.makeText(this,"name:" + name + "sex:" + sex,Toast.LENGTH_SHORT).show();
}
}
运行
就只是封装一下,嗯。
47.3 传递Serializable接口
创建一个Student类
package com.dingjiaxiong.myintent.simple01;
import java.io.Serializable;
//必须实现这个接口,才能传递
public class Student implements Serializable {
public int id;
public String name;
public int age;
}
1
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.dingjiaxiong.myintent.R;
public class MainActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
public void startAction(View view) {
Intent intent = new Intent(this,MainActivity2.class);
Student student = new Student();
student.id = 9;
student.name = "dingjiaxiong";
student.age = 22;
intent.putExtra("Student",student);
startActivity(intent);
}
}
2
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.dingjiaxiong.myintent.R;
import java.io.Serializable;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收对象
Intent intent = getIntent();
Student student = (Student) intent.getSerializableExtra("Student");
Toast.makeText(this,"student.id:" + student.id + "student.name:" + student.name,Toast.LENGTH_SHORT).show();
}
}
运行
47.4 传递Parcelable接口
Student.java
package com.dingjiaxiong.myintent.simple01;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
//成为Parcelable子类
public class Student implements Parcelable {
public Student(){}
public String name;
public int age;
protected Student(Parcel in) {
name = in.readString();
age = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
@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];
}
};
}
1
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.dingjiaxiong.myintent.R;
public class MainActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
public void startAction(View view) {
Intent intent = new Intent(this,MainActivity2.class);
//传递Parceable对象
Student student = new Student();
student.name = "dingjiaxiong";
student.age = 22;
intent.putExtra("student",student);
startActivity(intent);
}
}
2
package com.dingjiaxiong.myintent.simple01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.dingjiaxiong.myintent.R;
import java.io.Serializable;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收对象
Intent intent = getIntent();
Student student = intent.getParcelableExtra("student");
Toast.makeText(this,"student.age:" + student.age + "student.name:" + student.name,Toast.LENGTH_SHORT).show();
}
}
运行
最好是使用Parcelable。因为其兼容安卓虚拟机,比Ser性能高很多。
标签:dingjiaxiong,47,Intent,import,intent,android,public From: https://www.cnblogs.com/55zjc/p/16709773.html