适合Android初学者的第一个简单案例(页面跳转)

一、案例描述

创建两个页面,一个界面作为程序的入口展示界面,在本项目中成为当前页面,另一个页面为跳转之后的页面,称为跳转页面,本项目可以实现从当前页面,定时3秒后,自动跳转到跳转页面。

二、代码实现

1.当前页面的类

当前页面路径如下(这是我的路径,只是作为参考,要以实际创建项目为准):src/main/java/com.sut.myapplication/ActivityThis.java

package com.sut.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ActivityThis extends AppCompatActivity {
    @Override
    protected void onResume(){
        super.onResume();
        goNextPage();//跳转到下一界面
    }
    //跳转到下一界面
    private void goNextPage(){
        TextView textView  = findViewById(R.id.tv_this);
        textView.setText("3秒后进入下一界面");
        //延迟三秒(3000毫秒)后启动任务mGoNext
        new Handler().postDelayed(mGoNext,3000);
    }

    private Runnable mGoNext = new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(ActivityThis.this,ActivityOther.class));
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_this);
    }
}

onCreate() 方法被重写,当活动被创建时系统会调用该方法。在这里,首先调用了父类的 onCreate() 方法,然后通过setContentView方法设置了当前活动的布局为 R.layout.activity_this。onResume() 方法被重写,系统会调用该方法。在这里,首先调用了父类的 onResume() 方法,然后调用了getNextPage方法。这两个方法是两个活动(activity),这也就是为什么该方法能主动被调用的原因,onResume() 方法,当活动即将进入前台并变为用户可见状态时调用,getNextPage方法是自己创建的方法,其中写了一个Textview的对象,用来指定其中所使用的xml文件中的Textview中的id,并给该文本赋值,然后调用一个Handler里面的一个延时方法,最后使用这是一个实现了 Runnable 接口的匿名内部类对象。在其中的 run() 方法中定义了跳转到下一个界面的操作,即启动一个新的 Activity(ActivityOther),其中this表示当前页面,this前的类名可以省略,class前的类名表示跳转类的类名,不可省略。

2.跳转页面的类

跳转页面路径如下:src/main/java/com.sut.myapplication/ActivityOther.java

package com.sut.myapplication;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ActivityOther extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
    }
}

该类就是当触发时产生页面跳转的方法,在1中有讲。 

3.当前页面的xml文件

当前页面路径如下:src/main/res/layout/activity_this.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_this"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </TextView>
</LinearLayout>

一个页面布局文件,这里不做详细解释,就是一些页面布局的写法,我在日后的博文中,会详细讲这些。 

4.跳转页面的xml文件

跳转页面路径如下:src/main/res/layout/activity_other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎来到第二个页面!"
        >
    </TextView>
</LinearLayout>

一个页面布局文件,这里不做详细解释,就是一些页面布局的写法,我在日后的博文中,会详细讲这些。  

5.配置文件

跳转页面路径如下:src/main/AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sut.myapplication">

    <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.HelloWorld">
        <activity android:name=".ActivityThis">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ActivityOther">
        </activity>
    </application>

</manifest>

 这个是程序入口的配置文件,主要是activity的配置。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

麦芒疯狂生长!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值