編輯:關於 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 則在倒計時結束時觸發,非常適合做倒數顯示。
閃屏(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 則更為輕量,適合單純的定時跳轉。開發者可根據實際需求靈活選用。
Android 學習筆記:RadioButton 單選按鈕詳解
RadioButton 是 Android 中用來做單選的常用控件,通常會搭配 RadioGroup 使用,確保同一組內只能選中一項。以下透過範例說明它的布局與事件處理。
百度地圖 SDK v3.0.0 實戰(二):定位與方向感測器整合
在上一篇 Blog 中,我們成功地把地圖導入專案。本篇則要為地圖加上兩個實用功能:第一是定位;第二是與方向感測器結合,透過旋轉手機來確認道路方向。有了這兩個功能,地圖就能
Android設計模式之工廠方法模式解析
模式意圖工廠方法模式的核心思想是:定義一個創建對象的接口,讓子類決定具體實例化哪一個類。它把對象的創建過程延遲到子類,從而使系統更符合開閉原則。在Android源碼中,工
智能硬件開發入門:用 Arduino 驅動直流馬達
第 6 節 直流馬達 馬達是我們最常打交道的電動裝置之一,它把電能轉化為動能,驅動其他部件運動。聽起來抽象,舉幾個例子就明白了:小時候玩的四驅車裡帶動輪子轉動的小東西就是