导航菜单,点击按钮跳转。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"><LinearLayoutandroid:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"><Buttonandroid:text="随机数"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="toRandomNumber"android:id="@+id/random"/><Buttonandroid:text="日期时间"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="toDateTime"android:id="@+id/time"/><Buttonandroid:text="方向传感器"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="toSensor"android:id="@+id/sensor"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.weijun901.show;import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void toRandomNumber(View view) {Intent intent = new Intent(this, RandomNumber.class); // 替换为目标页面的类名startActivity(intent);}public void toDateTime(View view) {Intent intent = new Intent(this, DateTime.class); // 替换为目标页面的类名startActivity(intent);}public void toSensor(View view) {Intent intent = new Intent(this, Sensor.class); // 替换为目标页面的类名startActivity(intent);}
}