Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總
Android開發(fā)中經(jīng)常會有倒計(jì)時(shí)的功能,下面將總結(jié)出常見的集中實(shí)現(xiàn)方式。
1.直接使用Handler的消息機(jī)制來實(shí)現(xiàn)
xml布局中文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="開始計(jì)時(shí)" />
</LinearLayout>
java代碼如下:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends Activity{
private Button button;
private int count = 60;
private int COUNT_TIME = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
button = (Button) findViewById(R.id.button);
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(count <= 0){
count = 60;
button.setText("重新計(jì)時(shí)");
button.setClickable(true);
return;
}
count--;
button.setText(""+count);
sendEmptyMessageDelayed(COUNT_TIME,1000);
}
};
public void clickButton(View view){
handler.sendEmptyMessage(COUNT_TIME);
button.setClickable(false);
}
}
2.使用Timer和TimerTask,結(jié)合handler一起實(shí)現(xiàn)倒計(jì)時(shí)
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends Activity{
private Button button;
private int count = 30;
private int COUNT_TIME = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
button = (Button) findViewById(R.id.button);
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
button.setText(""+count);
if(count <= 0){
count = 30;
button.setClickable(true);
button.setText("重新計(jì)時(shí)");
timerTask.cancel(); //取消該任務(wù)
}
}
};
private Timer timer = new Timer();
private TimerTask timerTask;
public void clickButton(View view){
button.setClickable(false);
timerTask = new TimerTask() {
@Override
public void run() {
count--;
handler.sendEmptyMessage(COUNT_TIME);
}
};
timer.schedule(timerTask,0,1000); //0秒后,每過一秒鐘執(zhí)行一次該任務(wù)
}
@Override
protected void onDestroy() {
super.onDestroy();
//釋放資源
if(timerTask != null){
timerTask.cancel();
timerTask = null;
}
if(timer != null){
timer.cancel();
timer = null;
}
}
}
3.使用android自帶的原生倒計(jì)時(shí)類 CountDownTimer
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends Activity{
private Button button;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
button = (Button) findViewById(R.id.button);
}
public void clickButton(View view){
button.setClickable(false);
//第一個參數(shù):倒計(jì)時(shí)的毫秒數(shù);第二個參數(shù):接收onTick回調(diào)的時(shí)間間隔
timer = new CountDownTimer(30000, 10) {
public void onTick(long millisUntilFinished) {
button.setText(millisUntilFinished / 1000 + "秒");
}
public void onFinish() {
button.setText("重新計(jì)時(shí)");
button.setClickable(true);
}
};
timer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(timer != null){
timer.cancel();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
- Android 實(shí)現(xiàn)廣告歡迎界面(倒計(jì)時(shí))
- Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
- Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
- Android實(shí)現(xiàn)一個完美的倒計(jì)時(shí)功能
相關(guān)文章
一文詳解無痕埋點(diǎn)在Android中的實(shí)現(xiàn)
很多時(shí)候因?yàn)楫a(chǎn)品都會要獲取用戶的行為,需要客戶端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Android UI設(shè)計(jì)系列之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果(1)
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細(xì)介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)硭姆N方法,大家可以根據(jù)自己需要參考下2018-07-07
Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)
下面小編就為大家?guī)硪黄狝ndroid之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
android studio與手機(jī)連接調(diào)試步驟詳解
這篇文章主要為大家詳細(xì)介紹了android studio與手機(jī)連接調(diào)試步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
沉浸式就是要給用戶提供完全沉浸的體驗(yàn),使用戶有一種置身于虛擬世界之中的感覺。沉浸式模式就是整個屏幕中顯示都是應(yīng)用的內(nèi)容,沒有狀態(tài)欄也沒有導(dǎo)航欄,用戶不會被一些系統(tǒng)的界面元素所打擾,讓我們來實(shí)現(xiàn)下網(wǎng)上傳的沸沸揚(yáng)揚(yáng)的安卓沉浸式狀態(tài)欄2021-11-11

