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

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