一.代码
1. activity_main.xml
<?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"
app:layout_constraintBottom_toTopOf="@+id/radioGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<RadioButton
android:id="@+id/rb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="青海湖"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日月山" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="无图片"
tools:ignore="HardcodedText" />
</RadioGroup>
<Button
android:id="@+id/bt4"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:text="下一步"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. activity_main2.xml
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
tools:ignore="RtlHardcoded,SpUsage" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="270dp"
android:scaleType="fitStart"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
tools:ignore="ContentDescription" />
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返 回"
android:textSize="20dp"
android:layout_marginTop="10dp"
tools:ignore="HardcodedText,SpUsage" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.java
package com.example.pagechange;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
button = findViewById(R.id.bt4);
final Intent intent = new Intent(this, MainActivity2.class);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rb1) {
intent.putExtra("ID", "rb1");
} else if (i == R.id.rb2) {
intent.putExtra("ID", "rb2");
} else {
intent.putExtra("ID", "rb3");
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent);
}
});
}
}
4. MainActivity2.java
package com.example.pagechange;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = findViewById(R.id.textView);
imageView = findViewById(R.id.imageView);
Intent intent = getIntent();
String i = intent.getStringExtra("ID");
assert i != null;
if (i.equals("rb1")) {
textView.setText("欣赏迷人的:青海湖");
imageView.setImageResource(R.drawable.img1);
} else if (i.equals("rb2")) {
textView.setText("欣赏迷人的:日月山");
imageView.setImageResource(R.drawable.img2);
} else {
textView.setText("~~~");
imageView.setImageResource(R.drawable.img3);
}
button = findViewById(R.id.bt2);
button.setOnClickListener(view -> {
Intent backIntent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(backIntent);
});
}
}
二. 模拟器运行结果
注意:
图片要自己找图下载并重命名,本实验的三个图片分别是命名为 img1.jpg,img2.jpg,img3.jpg;(当然可以自己命名,注意代码中名字都对应上就可以哈)
图片放的位置是:app-res-drawable中,可以直接拖拽图片到drawable松开,图片自动存储到此位置。