頁面效果:
沒點擊跳過自然進入主頁,點擊跳過之后立即進入主頁HelloWorld
實現步驟:
1、歡迎頁布局activity_sp.xml放一張背景圖(圖片隨你便啦)再放一個盛放倒計時的TextView。
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:background="@drawable/a">
8
9 <RelativeLayout 10 android:layout_width="match_parent"
11 android:layout_height="wrap_content">
12
13 <TextView 14 android:id="@+id/tv"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_alignParentRight="true"
18 android:layout_marginRight="20dp"
19 android:layout_marginTop="10dp"
20 android:textSize="20sp" />
21 </RelativeLayout>
2、新建一個Activity用來關聯我們的layout布局文件
1 package com.cwj.test; 2
3 import android.content.Intent; 4 import android.os.Handler; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.WindowManager; 9 import android.widget.TextView; 10
11 import java.util.Timer; 12 import java.util.TimerTask; 13
14 public class SpActivity extends AppCompatActivity implements View.OnClickListener { 15
16 private int recLen = 5;//跳過倒計時提示5秒
17 private TextView tv; 18 Timer timer = new Timer(); //定義一個計時器 19 private Handler handler; 20 private Runnable runnable; 21
22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 //定義全屏參數
26 int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN; 27 //設置當前窗體為全屏顯示
28 getWindow().setFlags(flag, flag); 29 setContentView(R.layout.activity_sp); 30 initView(); 31 timer.schedule(task, 1000, 1000);//等待時間一秒,停頓時間一秒
32 /**
33 * 正常情況下不點擊跳過 34 */
35 handler = new Handler(); 36 handler.postDelayed(runnable = new Runnable() { 37 @Override 38 public void run() { 39 //從閃屏界面跳轉到首界面
40 Intent intent = new Intent(SpActivity.this, MainActivity.class); 41 startActivity(intent); 42 finish(); 43 } 44 }, 5000);//延遲5S后發送handler信息
45
46 } 47
48 private void initView() { 49 tv = findViewById(R.id.tv);//跳過
50 tv.setOnClickListener(this);//跳過監聽
51 } 52
53 TimerTask task = new TimerTask() { 54 @Override 55 public void run() { 56 runOnUiThread(new Runnable() { // UI thread
57 @Override 58 public void run() { 59 recLen--; 60 tv.setText("跳過 " + recLen); 61 if (recLen < 0) { 62 timer.cancel(); 63 tv.setVisibility(View.GONE);//倒計時到0隱藏字體
64 } 65 } 66 }); 67 } 68 }; 69
70 /**
71 * 點擊跳過 72 */
73 @Override 74 public void onClick(View view) { 75 switch (view.getId()) { 76 case R.id.tv: 77 //從閃屏界面跳轉到首界面
78 Intent intent = new Intent(SpActivity.this, MainActivity.class); 79 startActivity(intent); 80 finish(); 81 if (runnable != null) { 82 handler.removeCallbacks(runnable); 83 } 84 break; 85 default: 86 break; 87 } 88 }
3、我們還可以對樣式進行少許的修改,我們平常見到的歡迎界面是全屏的,所以我們在這里將標題欄隱藏掉
最簡單的方法:
在AndroidManifest.xml文件中添加
1 android:theme="@android:style/Theme.NoTitleBar"
以下是系統自帶的一些主題樣式,我們也可以對其進行部分修改
android:theme="@android:style/Theme.Dialog" 將一個Activity顯示為對話框模式 android:theme="@android:style/Theme.NoTitleBar" 不顯示應用程序標題欄 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不顯示應用程序標題欄,並全屏 android:theme="Theme.Light" 背景為白色 android:theme="Theme.Light.NoTitleBar" 白色背景並無標題欄 android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,無標題欄,全屏 android:theme="Theme.Black" 背景黑色 android:theme="Theme.Black.NoTitleBar" 黑色背景並無標題欄 android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,無標題欄,全屏 android:theme="Theme.Wallpaper" 用系統桌面為應用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" 用系統桌面為應用程序背景,且無標題欄 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系統桌面為應用程序背景,無標題欄,全屏 android:theme="Translucent" 透明背景 android:theme="Theme.Translucent.NoTitleBar" 透明背景並無標題 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 透明背景並無標題,全屏 android:theme="Theme.Panel" 面板風格顯示 android:theme="Theme.Light.Panel" 平板風格顯示
參考博客:
https://blog.csdn.net/juer2017/article/details/79069970