Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰:實現閃屏頁與倒計時自動跳轉

編輯:關於 Android 程式設計

在開發中經常需要實現閃屏頁(Splash 頁)與倒計時自動跳轉的功能。其實 Android 早已封裝好了 CountDownTimer 類,它把後台線程的創建與 Handler 隊列封裝為一個方便的類調用,讓我們不必再手寫倒計時邏輯。

閃屏頁除了用到 Handler 與 CountDownTimer 類外,還需要配置 Activity 的主題,這裡採用全屏主題:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"。

先看效果圖:

代碼如下:

package com.example.shanping;
import com.example.shanping.MyActivity.MyCountDownTimer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
public class MainActivity extends Activity {
private MyCountDownTimer mc;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
mc = new MyCountDownTimer(3000, 1000);
mc.start();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, MyActivity.class);
startActivity(intent);
}
}, 3000);
}
private Handler handler = new Handler();
/**
* 繼承 CountDownTimer
* 重寫父類的 onTick()、onFinish()
*/
class MyCountDownTimer extends CountDownTimer {
/**
* @param millisInFuture 以毫秒為單位的倒計時總數,例如 1000 表示 1 秒
* @param countDownInterval 間隔多少毫秒調用一次 onTick,例如 1000 表示每秒調用一次
*/
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
tv.setText("正在跳轉");
}
public void onTick(long millisUntilFinished) {
tv.setText("倒計時(" + millisUntilFinished / 1000 + ")");
}
}
}

其中 CountDownTimer 的兩個構造參數分別為倒計時總時長與計時間隔,onTick 在每次倒計時時被調用,onFinish 則在倒計時結束時觸發,非常適合做倒數顯示。

用 Handler 實現閃屏跳轉

閃屏(SplashScreen),也稱啟動畫面,就是在啟動時短暫展示幾秒後自動關閉。Android 實現起來非常簡單,使用 Handler 的 postDelayed 方法即可。該方法傳入一個 Runnable 對象與一個延遲時間(單位毫秒),實現延遲執行的效果:Runnable 中包含延遲後要執行的操作,第二個參數指定延遲時長。

Java 代碼如下:

package com.mstar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class ActSplashScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shan);
// 閃屏的核心代碼
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ActSplashScreen.this, DialogTest.class);
// 從啟動動畫界面跳轉到主界面
startActivity(intent);
ActSplashScreen.this.finish();// 結束啟動動畫界面
}
}, 3000);// 啟動動畫持續 3 秒
}
}

對應的布局代碼:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="閃一下" >
</TextView>
</LinearLayout>

兩種方式各有所長:CountDownTimer 適合需要在倒計時過程中實時更新界面(如顯示剩餘秒數)的場景;而 Handler 的 postDelayed 則更為輕量,適合單純的定時跳轉。開發者可根據實際需求靈活選用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved