Android app欢迎页面停留几秒的实现

        很多时候我们登录一个APP会在开始的一个 图片页面停留一两秒,对也这个我们在开发APP时可以进行很简单的编写


代码如下:

写在MainActivity.class中实现对Main2Activity.class的跳转

package org.test.com.test1;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import static org.test.com.test1.R.id.start;

public class MainActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGHT = 3000;  //延迟3秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                MainActivity.this.startActivity(intent);
                MainActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}
还需要一个activity_main.xml,这个只需要简简单单地添加一个背景图片就行


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@mipmap/login_backgrond_1"       //添加图片
    tools:context="org.test.com.test1.MainActivity">
</LinearLayout>


  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的在 Android Studio 中写一个欢迎页面停留三秒的代码示例: 1. 在 res/layout 目录下创建一个名为 activity_welcome.xml 的布局文件,用于设置欢迎页面的布局,如下所示: ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/welcome_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎来到我的应用!" android:textSize="24sp" android:layout_centerInParent="true"/> </RelativeLayout> ``` 2. 在 app/src/main/java 目录下创建一个名为 WelcomeActivity 的 Activity 类,用于控制欢迎页面的逻辑,如下所示: ```java import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; public class WelcomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); // 延迟 3 秒后跳转到主页面 new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(WelcomeActivity.this, MainActivity.class); startActivity(intent); finish(); } }, 3000); } } ``` 3. 在 AndroidManifest.xml 中注册 WelcomeActivity,如下所示: ```xml <activity android:name=".WelcomeActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 完成以上步骤后,您可以运行应用程序并查看欢迎页面页面将显示“欢迎来到我的应用!”字样,并在 3 秒后自动跳转到 MainActivity。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

三岁牧羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值