Day29 - Activity & Intent跳页

我们都知道APP一般涛说不会只有一个页面
通常都有按钮可以跳到另一页或是跳回原本那页
Android的APP通常是由多个Activity组成
一个Activity其实可以代表一个画面

今天就来新增一个Activity,也就是一个新的页面
并且透过Intent来转跳画页

开始

  • 第一页
    xml档:activity_main.xml
    java档:MainActivity.java

  • 第二页
    xml档:activity_two.xml
    java档:Two.java

先来新增第二个页面八

  • 到左边目录栏java -> 右键com.example.intent -> New -> Activity ->Empty Activity 点下去
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769oATx7ixney.png
  • 把Activity Name 改为:Two,按下Finish
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769hnWcVUTUIJ.png
  • 目录页就会多Two.java、 activity_two.xml
    https://ithelp.ithome.com.tw/upload/images/20211005/201417692OsXw4cTAU.png

第一页

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">
    <Button
        android:id="@+id/btn_totwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳第二页"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

新增Intent
Intent intent = new Intent(MainActivity.this,想要跳到的页面)

Intent intent = new Intent(MainActivity.this,Two.class);

之後再启动intent跳页

startActivity(intent);

完整程序:

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button btn_totwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_totwo = findViewById(R.id.btn_totwo);
        btn_totwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Two.class);
                startActivity(intent);
            }
        });
    }
}

第二页

activity_two.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=".Two"
    android:background="@color/black">
    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳回去"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Two.java

只需要在点击事件内加上finish()即可
finish():结束此页面,跳回上一个页面
完整程序:

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Two extends AppCompatActivity {
    Button btn_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

执行结果:

  • 初始画面
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769eNyganFk4v.png
  • 按下跳第二页,跳到Two页
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769SifMgvdylR.png
  • 按下跳回去,回第一页
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769p6j2dDcu6I.png

<<:  虹语岚访仲夏夜-26(专业的小四篇-终)

>>:  [第25天]理财达人Mx. Ada-ADX指标

用 Line LIFF APP 实现信箱验证绑定功能(2) - 使用 Vite 快速打造输入页面

昨天提到,LIFF APP 有可能因为使用者没有绑定 email,或是不授权 email 使用导致无...

Day 2 - Home Lab 事前准备 - 安装篇

在 Day 1 时,我们有稍微提到了 Home Lab 可以使用一般的桌上型电脑来建置,那今天我们要...

javascript函式的变形2

现在我们来学习函式的进阶,全域变数跟区域变数的差别和使用方法。区域变数的差别和使用方法。 ...

不用Recoil的话,如何自己制作一个 Custom hook 来共享全域变数?(2)

实作自己的全域 count, setCount codesandbox demo const { c...

AZ-304 Practice Exam - The key to Transform Failure into Success

Azure Solutions Architect Expert AZ-304 certificat...