Android實(shí)現(xiàn)倒計(jì)時方法匯總
Android開發(fā)中經(jīng)常會有倒計(jì)時的功能,下面將總結(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ì)時" /> </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ì)時"); 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ì)時
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ì)時"); 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ì)時類 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ù):接收onTick回調(diào)的時間間隔 timer = new CountDownTimer(30000, 10) { public void onTick(long millisUntilFinished) { button.setText(millisUntilFinished / 1000 + "秒"); } public void onFinish() { button.setText("重新計(jì)時"); button.setClickable(true); } }; timer.start(); } @Override protected void onDestroy() { super.onDestroy(); if(timer != null){ timer.cancel(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時控件示例
- android實(shí)現(xiàn)倒計(jì)時功能代碼
- Android實(shí)現(xiàn)計(jì)時與倒計(jì)時的常用方法小結(jié)
- Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時跳轉(zhuǎn)實(shí)例代碼
- Android自定義圓形倒計(jì)時進(jìn)度條
- Android實(shí)現(xiàn)時間倒計(jì)時功能
- Android 實(shí)現(xiàn)廣告歡迎界面(倒計(jì)時)
- Android中Handler實(shí)現(xiàn)倒計(jì)時的兩種方式
- Android定時器和倒計(jì)時實(shí)現(xiàn)淘寶秒殺功能
- Android實(shí)現(xiàn)一個完美的倒計(jì)時功能
相關(guān)文章
Android App中的多個LinearLayout嵌套布局實(shí)例解析
這篇文章主要介紹了Android App中的多個LinearLayout嵌套布局實(shí)例,利用線性布局來排列按鈕是安卓應(yīng)用布局中的常用做法,需要的朋友可以參考下2016-04-04android實(shí)現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實(shí)現(xiàn)視頻的加密和解密(使用AES),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05android幾種不同對話框的實(shí)現(xiàn)方式
這篇文章介紹了android幾種不同對話框的實(shí)現(xiàn),主要包括:1、顯示提示消息的對話框.2、簡單列表項(xiàng)對話框。3、單選列表項(xiàng)對話框。4、多選列表對話框。5、自定義列表項(xiàng)對話框。6、自定義View的對話框,需要的朋友可以參考下2015-08-08Android6.0動態(tài)申請權(quán)限所遇到的問題小結(jié)
這篇文章給大家介紹了Android6.0動態(tài)申請權(quán)限所遇到的問題,在沒給大家介紹這下問題之前,先給大家說下基本定義和基本使用方式,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,對android 6.0 動態(tài)權(quán)限遇到問題感興趣的朋友一起看看吧2016-11-11android TextView不用ScrollViewe也可以滾動的方法
這篇文章主要介紹了android TextView不用ScrollViewe也可以滾動的方法,很簡單實(shí)用的代碼,大家參考使用吧2013-11-11Android鬧鈴服務(wù)AlarmManager用法深入分析
這篇文章主要介紹了Android鬧鈴服務(wù)AlarmManager用法,結(jié)合實(shí)例形式深入分析了鬧鈴服務(wù)AlarmManager的功能、原理、定義與使用方法,需要的朋友可以參考下2016-08-08

viewpager實(shí)現(xiàn)自動循環(huán)輪播圖

Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果