Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼,相信都看得懂。
Android已經(jīng)幫封裝好了一個(gè)類,只不過(guò)很多人不知道而已。
代碼:
public class SplashActivity extends BaseAppCompatActivity {
@InjectView(R.id.ivBg)
ImageView ivBg;
@InjectView(R.id.tvSkip)
TextView tvSkip;
int[] imgs = new int[]{
R.mipmap.irving,
R.mipmap.bryant,
R.mipmap.james,
R.mipmap.harden,
R.mipmap.curry};
private CountDownTimer timer;
@Override
protected int getContentViewLayoutID() {
return R.layout.activity_splash;
}
@Override
protected void initViewsAndEvents() {
int index = (int) (Math.random() * imgs.length);
ivBg.setImageResource(imgs[index]);
timer = new CountDownTimer(3500, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvSkip.setText(String.format(getResources().getString(R.string.skip), (int) (millisUntilFinished / 1000 + 0.1)));
}
@Override
public void onFinish() {
tvSkip.setText(String.format(getResources().getString(R.string.skip), 0));
startActivity(new Intent(mContext, HomeActivity.class));
finish();
}
};
timer.start();
}
@OnClick(R.id.tvSkip)
public void skip() {
if (timer != null)
timer.cancel();
startActivity(new Intent(mContext, HomeActivity.class));
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
}
}
調(diào)用的時(shí)候很簡(jiǎn)單:timer.start();
最后說(shuō)明一下:CountDownTimer timer = new CountDownTimer(3500, 1000)中,第一個(gè)參數(shù)表示總時(shí)間,第二個(gè)參數(shù)表示間隔時(shí)間。意思就是每隔一秒會(huì)回調(diào)一次方法onTick,然后10秒之后會(huì)回調(diào)onFinish方法。
布局activity_splash.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">
<ImageView
android:id="@+id/ivBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tvSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:background="@drawable/common_button_selector"
android:padding="5dp"
android:text="@string/skip" />
</RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)單實(shí)現(xiàn)Android滾動(dòng)公告欄
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)Android滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android 代碼設(shè)置開機(jī)自啟動(dòng)App的方法
今天小編就為大家分享一篇Android 代碼設(shè)置開機(jī)自啟動(dòng)App的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
非常實(shí)用的小功能 Android應(yīng)用版本的更新實(shí)例
這篇文章主要為大家詳細(xì)介紹了一個(gè)非常實(shí)用的小功能,Android應(yīng)用版本的更新實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08
Android TextView中文字通過(guò)SpannableString設(shè)置屬性用法示例
這篇文章主要介紹了Android TextView中文字通過(guò)SpannableString設(shè)置屬性用法,結(jié)合實(shí)例形式分析了TextView控件中SpannableString類相關(guān)屬性的使用技巧,需要的朋友可以參考下2016-08-08
Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼
這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
完美解決Android App啟動(dòng)頁(yè)有白屏閃過(guò)的問題
這篇文章主要介紹了完美解決Android App啟動(dòng)頁(yè)有白屏閃過(guò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
android如何設(shè)置小區(qū)廣播默認(rèn)信道(50與60并支持雙卡)
置小區(qū)廣播默認(rèn)信道50與60,并支持雙卡主要是印度市場(chǎng),具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android wifi 調(diào)試詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android wifi 調(diào)試詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02

