Button button2=findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity2.this,MainActivity.class);
startActivity(intent);
}
});
方法二:finish返回
使用finish方法,将刚刚第二个页面销毁
============================================================================
如果使用intent返回到第一页页面,退出程序需要按三下back键
如果使用finish销毁第二个页面,退出程序需要按一下back键
===============================================================================
传递普通数据
1.activity1向activity2传递数据
2.activity2接收数据
核心代码:
activity1xml:
<?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=“.MainActivity”>
<TextView
android:id=“@+id/textView”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“这是第一个页面”
android:textSize=“20dp”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“20dp”
android:layout_marginTop=“55dp”
android:text=“跳转”
app:layout_constraintStart_toStartOf=“@+id/textView”
app:layout_constraintTop_toBottomOf=“@+id/textView” />
<EditText
android:id=“@+id/editText”
android:layout_width=“169dp”
android:layout_height=“44dp”
android:layout_marginStart=“88dp”
android:layout_marginTop=“164dp”
android:layout_marginEnd=“88dp”
android:hint=“请输入”
android:inputType=“text”
app:layout_constraintEnd_toEndOf=“@+id/textView”
app:layout_constraintHorizontal_bias=“0.0”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
activity2xml代码:
<?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=“.MainActivity2”>
<TextView
android:id=“@+id/textView2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“87dp”
android:layout_marginTop=“57dp”
android:text=“这是第二个页面”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“99dp”
android:layout_marginTop=“192dp”
android:text=“返回”
app:backgroundTint=“@color/teal_200”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<TextView
android:id=“@+id/textView3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“121dp”
android:layout_marginTop=“101dp”
android:text=“TextView”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/button2” />
</androidx.constraintlayout.widget.ConstraintLayout>
3.运行结果:
传递对象数据
1.创建实体类实现Serializable接口:
2.activity1向activity2传递数据:
3.activity2接收数据:
4.运行结果:
核心代码:
activity2xml:
<?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=“.MainActivity2”>
<TextView
android:id=“@+id/textView2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“87dp”
android:layout_marginTop=“57dp”
android:text=“这是第二个页面”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“99dp”
android:layout_marginTop=“192dp”
android:text=“返回”
app:
backgroundTint=“@color/teal_200”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<TextView
android:id=“@+id/textView3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“121dp”
android:layout_marginTop=“101dp”
android:text=“TextView”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/button2” />
<TextView
android:id=“@+id/textView4”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”
android:layout_marginEnd=“1dp”
android:text=“TextView”
app:layout_constraintEnd_toEndOf=“@+id/textView3”
app:layout_constraintTop_toBottomOf=“@+id/textView3” />
</androidx.constraintlayout.widget.ConstraintLayout>
activity1代码:
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Phone phone=new Phone();
phone.name=“HUAWEIMate40”;
phone.price=6088;
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra(“key”,phone);
startActivity(intent);
}
});
activity2代码:
Phone phone= (Phone) getIntent().getSerializableExtra(“key”);//强制类型转换
TextView textView=findViewById(R.id.textView3);
TextView textView1=findViewById(R.id.textView4);
textView.setText(String.format(phone.name));
textView1.setText(String.format(String.valueOf(phone.price)));
=====================================================================
1.activity1 注意和上面的代码区别
2.activity2页面回传
3.运行结果
核心代码:
activity1xml:
<?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=“.MainActivity”>
<TextView
android:id=“@+id/textView”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“这是第一个页面”
android:textSize=“20dp”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“20dp”
android:layout_marginTop=“55dp”
android:text=“跳转”
app:layout_constraintStart_toStartOf=“@+id/textView”
app:layout_constraintTop_toBottomOf=“@+id/textView” />
<TextView
android:id=“@+id/textView5”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“54dp”
android:layout_marginEnd=“9dp”
android:text=“TextView”
android:inputType=“number”
app:layout_constraintEnd_toEndOf=“@+id/button”
app:layout_constraintTop_toBottomOf=“@+id/button” />
</androidx.constraintlayout.widget.ConstraintLayout>
activity2xml:
<?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=“.MainActivity2”>
<TextView
android:id=“@+id/textView2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“87dp”
android:layout_marginTop=“57dp”
android:text=“这是第二个页面”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“99dp”
android:layout_marginTop=“192dp”
android:text=“返回”
app:backgroundTint=“@color/teal_200”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<EditText
android:id=“@+id/editText”
android:layout_width=“200dp”
android:layout_height=“wrap_content”
android:hint=“请输入”
android:layout_marginStart=“87dp”
android:layout_marginTop=“89dp”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/button2” />
</androidx.constraintlayout.widget.ConstraintLayout>
activity1核心代码