导航菜单,点击按钮跳转。
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="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"
>
<Button
android:text="随机数"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toRandomNumber"
android:id="@+id/random"/>
<Button
android:text="日期时间"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toDateTime"
android:id="@+id/time"/>
<Button
android: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 {
@Override
protected 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);
}
}