我们都知道APP一般涛说不会只有一个页面
通常都有按钮可以跳到另一页或是跳回原本那页
Android的APP通常是由多个Activity组成
一个Activity其实可以代表一个画面
今天就来新增一个Activity,也就是一个新的页面
并且透过Intent来转跳画页
第一页
xml档:activity_main.xml
java档:MainActivity.java
第二页
xml档:activity_two.xml
java档:Two.java
先来新增第二个页面八
只需要拉一个按钮来实现跳页功能
<?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>
新增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);
}
});
}
}
一样拉一个按钮
这是要跳回上一页的按钮
并且把页面背景改为黑色
这样比较好辨别
<?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>
只需要在点击事件内加上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();
}
});
}
}
执行结果:
昨天提到,LIFF APP 有可能因为使用者没有绑定 email,或是不授权 email 使用导致无...
在 Day 1 时,我们有稍微提到了 Home Lab 可以使用一般的桌上型电脑来建置,那今天我们要...
现在我们来学习函式的进阶,全域变数跟区域变数的差别和使用方法。区域变数的差别和使用方法。 ...
实作自己的全域 count, setCount codesandbox demo const { c...
Azure Solutions Architect Expert AZ-304 certificat...