IntelliJ IDEA 安卓点击按钮页面跳转

利用监听按钮点击进行页面跳转,其他方法还没有测试

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,
默认都是按照顺序访问的,你启动应用程序,首先访问的页面应放在最上面,这样运行的时候才会先运行它,如果不在这里面注册页面的话,也是不会跳转的。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值