利用监听按钮点击进行页面跳转,其他方法还没有测试
A页面(MainActivity)跳转到B页面(Menu)
A页面
前端布局
关键是设置按钮id,利用id监听进行页面跳转
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hello"
android:gravity="center"
android:text="Hello World!"
android:textSize="50sp"
android:textColor="#E91E63"/>
<Button
android:text="@string/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_change"/>
<Button
android:text="@string/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_toMenu"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
后端逻辑
MainActivity
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//以上内容属于页面初始化
//点击按钮生成随机数
TextView hello = findViewById(R.id.hello);
Button btn_change = findViewById(R.id.btn_change);
btn_change.setOnClickListener(v -> {
int num = (int) (Math.random() * 100);
hello.setText(String.valueOf(num));
});
//点击按钮跳转页面
Button btn_toMenu = findViewById(R.id.btn_toMenu);
btn_toMenu.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, Menu.class);
startActivity(intent);
});
}
}
B页面(Menu)跳转到A页面(MainActivity)
B页面
前端布局
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Menu">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:text="退出"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btn_toMain"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
后端逻辑
Menu
import android.content.Intent;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Menu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//点击按钮跳转到首页
Button btn_toMain = findViewById(R.id.btn_toMain);
btn_toMain.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, MainActivity.class);
startActivity(intent);
});
}
}
当在B页面点击退出,可见A页面被销毁后又重新建立
然而在B页面点击返回按钮,可见A页面并没有被销毁
所以,这里要注意一下生命周期的运用。
重要提示AndroidManifest.xml
应用程序配置文件
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.weijun901.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.App">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
如果你建立了两个页面或多个页面,也就是两个或多个activity,
默认都是按照顺序访问的,你启动应用程序,首先访问的页面应放在最上面
,这样运行的时候才会先运行它,如果不在这里面注册
页面的话,也是不会跳转的。