日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

干货三:CountDownTimer倒计时工具类

發(fā)布時(shí)間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 干货三:CountDownTimer倒计时工具类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

概述

毫無(wú)疑問(wèn),我們先來(lái)看下官方文檔中給的介紹

CountDownTimer

官方定義如下:

Schedule a countdown until a time in the future, with regular
notifications on intervals along the way.

同時(shí)官方也給出了使用的demo:

構(gòu)造函數(shù) 方法 以及參數(shù)含義請(qǐng)參考官方文檔,已經(jīng)很明確的說(shuō)明了,這里就不重復(fù)了~

Code

import android.os.Bundle; import android.os.CountDownTimer; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button;import com.turing.base.R;/*** 倒計(jì)時(shí)演示* <p/>* Android中有個(gè)countDownTimer類,* 從名字上就可以看出來(lái),它的功能是記錄下載時(shí)間,* 將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的調(diào)用.* <p/>* CountDownTimer由系統(tǒng)提供,果斷拋棄了自己以前使用Handler更新UI的做法*/ public class CountDownActivity extends AppCompatActivity {private MyCountDownTimer mc;private Button countBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_count_down);countBtn = (Button) findViewById(R.id.show);// 共計(jì)30S,1S調(diào)用一次onTickmc = new MyCountDownTimer(30000, 1000);mc.start();}public void oncancel(View view) {mc.cancel();}public void restart(View view) {mc.start();}/*** 自定義倒計(jì)時(shí)類*/class MyCountDownTimer extends CountDownTimer {/*** @param millisInFuture 表示以毫秒為單位 倒計(jì)時(shí)的總數(shù)* <p/>* 例如 millisInFuture=1000 表示1秒* @param countDownInterval 表示 間隔 多少微秒 調(diào)用一次 onTick 方法* <p/>* 例如: countDownInterval =1000 ;* 表示每1000毫秒調(diào)用一次onTick()*/public MyCountDownTimer(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}@Overridepublic void onTick(long millisUntilFinished) {countBtn.setText("倒計(jì)時(shí)(" + millisUntilFinished / 1000 + ")...");}@Overridepublic void onFinish() {countBtn.setText("done");}} }

運(yùn)行圖

CountDownTimer源碼

/** Copyright (C) 2008 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.os;/*** Schedule a countdown until a time in the future, with* regular notifications on intervals along the way.** Example of showing a 30 second countdown in a text field:** <pre class="prettyprint">* new CountDownTimer(30000, 1000) {** public void onTick(long millisUntilFinished) {* mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);* }** public void onFinish() {* mTextField.setText("done!");* }* }.start();* </pre>** The calls to {@link #onTick(long)} are synchronized to this object so that* one call to {@link #onTick(long)} won't ever occur before the previous* callback is complete. This is only relevant when the implementation of* {@link #onTick(long)} takes an amount of time to execute that is significant* compared to the countdown interval.*/ public abstract class CountDownTimer {/*** Millis since epoch when alarm should stop.*/private final long mMillisInFuture;/*** The interval in millis that the user receives callbacks*/private final long mCountdownInterval;private long mStopTimeInFuture;/*** boolean representing if the timer was cancelled*/private boolean mCancelled = false;/*** @param millisInFuture The number of millis in the future from the call* to {@link #start()} until the countdown is done and {@link #onFinish()}* is called.* @param countDownInterval The interval along the way to receive* {@link #onTick(long)} callbacks.*/public CountDownTimer(long millisInFuture, long countDownInterval) {mMillisInFuture = millisInFuture;mCountdownInterval = countDownInterval;}/*** Cancel the countdown.*/public synchronized final void cancel() {mCancelled = true;mHandler.removeMessages(MSG);}/*** Start the countdown.*/public synchronized final CountDownTimer start() {mCancelled = false;if (mMillisInFuture <= 0) {onFinish();return this;}mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;mHandler.sendMessage(mHandler.obtainMessage(MSG));return this;}/*** Callback fired on regular interval.* @param millisUntilFinished The amount of time until finished.*/public abstract void onTick(long millisUntilFinished);/*** Callback fired when the time is up.*/public abstract void onFinish();private static final int MSG = 1;// handles counting downprivate Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {synchronized (CountDownTimer.this) {if (mCancelled) {return;}final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();if (millisLeft <= 0) {onFinish();} else if (millisLeft < mCountdownInterval) {// no tick, just delay until donesendMessageDelayed(obtainMessage(MSG), millisLeft);} else {long lastTickStart = SystemClock.elapsedRealtime();onTick(millisLeft);// take into account user's onTick taking time to executelong delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();// special case: user's onTick took more than interval to// complete, skip to next intervalwhile (delay < 0) delay += mCountdownInterval;sendMessageDelayed(obtainMessage(MSG), delay);}}}}; }

總結(jié)

以上是生活随笔為你收集整理的干货三:CountDownTimer倒计时工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 美女激情av| 日韩中文字幕在线视频 | 成人性生活毛片 | 黑白配高清国语在线观看 | 国产精品无码一区二区三区三 | 尤物av无码色av无码 | 国产精品中文在线 | 国产91精品一区二区绿帽 | 国产精品视频网站 | 狂野欧美性猛交xxxx777 | 成人精品一区日本无码网 | 色爱综合网 | 色导航在线 | 女生毛片 | 91福利在线看 | 三级国产在线 | 欧美性爱精品在线 | 福利视频不卡 | 亚洲爆爽av | 色鬼久久 | 欧美日韩精品一二三区 | 亚洲在线免费 | 森泽佳奈在线播放 | 少妇在线视频 | 欧美午夜小视频 | 久久久久在线观看 | 在线只有精品 | xxxxⅹxxxhd日本8hd | 91亚洲国产成人精品性色 | 天堂成人国产精品一区 | 67194少妇| 国产第四页 | 黄色在线不卡 | 欧美一区视频在线 | 午夜视频一区二区三区 | 亚洲一区二区三区视频 | 精久久久久 | jzzijzzij亚洲成熟少妇在线播放 狠狠躁日日躁夜夜躁2022麻豆 | 91美女片黄在线观看 | 午夜视频免费在线 | 久久久久97国产 | 天天干中文字幕 | 91在线超碰 | 婷婷影音 | 人妖和人妖互交性xxxx视频 | 蜜桃麻豆视频 | 在线观看视频 | 国产乱子一区二区 | 美国少妇性做爰 | 一级生活毛片 | 人人妻人人爽欧美成人一区 | 操穴网站| 人妻av无码一区二区三区 | 亚洲免费国产 | 阿v天堂2014| 少妇xxxx| 欧美精品1区 | 黑丝扣逼| 不卡久久 | 欧美性生活视频 | 美女极度色诱图片www视频 | av成人免费在线观看 | 亚洲国产网址 | 亚洲高清视频网站 | 欧美xxxxx精品 | av网站免费观看 | 无码精品一区二区三区AV | 亚洲911精品成人18网站 | 国产18一19sex性护士 | 五月六月婷婷 | 亚洲天堂中文字幕 | 中文字幕日韩无 | 可以免费看的av毛片 | 午夜色av| 国产成人久久婷婷精品流白浆 | 中国黄色片视频 | 伊人久久久 | 亚洲图片中文字幕 | 亚洲伦理在线 | 又爽又黄又无遮挡 | 黄色.com| 亚洲+小说+欧美+激情+另类 | 夜夜操免费视频 | 国产一区二区视频在线播放 | 精一区二区 | 国产黄色网络 | 日韩在线精品强乱中文字幕 | 女人免费视频 | 精品日韩在线观看 | 香蕉视频97 | 亚洲xx站| 九九热视频在线观看 | 国产在线观看成人 | 黄色片子网站 | 91嫩草欧美久久久九九九 | 欧美不卡视频在线观看 | 国产精品国产三级国产aⅴ 欧美bbbbbbbbbbbb18av | 欧美成人免费观看 | 欧美熟妇久久久久 |