前面為大家講過計時器的順時針的兩種方法,在錄制視頻等操作中頗有使用,今天就給大家帶來倒計時實現的兩種方式。
對面前面的正向計時方法沒有了解的,可以直接傳送門:http://www.cnblogs.com/liushilin/p/5802954.html
雖然最近寫的都比較簡單和基礎,不過簡單不代表熟悉,基礎不代表就會,大牛繞過,哈,中牛小牛也可以繞過,這個是寫給初學者的。
先搞個效果圖。
代碼實現方式也超級簡單啦,這里首推第一種實現方式,而且也是比較適合大家的,就是通過直接繼承CountDownTimer來實現。
對於CountDownTimer這個類很簡單,繼承它的時候必須重寫構造方法和實現其虛擬方法。
構造方法的兩個參數分別是(倒計時開始時間,間隔時間)
另外兩個方法分別是onTick(現在還剩的時間),計時結束后你想做的時間可以在onFinish()中做。
值的注意的是,所有的時間都是以毫秒形式來做的,所以在你使用的時候要記得整除1000取商。
不過由於我使用的是私有內部類的方式對外部類存在引用,為了防止內存泄漏,在Activity銷毀的時候應該注意對其置空,同樣我們也應該避免重復創建對象。
另外一種方式還是使用我們常用的Handler + Thread的方式來實現。不過實現的時候同樣要非常小心內存泄漏,因為如果用戶在銷毀Activity的時候應該注意讓其計時子線程不再循環,這個可以通過設置一個tag標簽對其判斷。
這樣在銷毀的時候把這個tag標簽置為false,結束線程的執行!
下面是實現代碼:
1 package com.example.nanchen.timerdemo; 2 3 import android.os.Bundle; 4 import android.os.CountDownTimer; 5 import android.os.Handler; 6 import android.os.Message; 7 import android.support.v7.app.AppCompatActivity; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends AppCompatActivity { 13 14 private Button mBtnGetCode; 15 private TimeCount mTimeCount; 16 private Button mBtnGetCode2; 17 private boolean timeFlag = true; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 mBtnGetCode = (Button) findViewById(R.id.main_btn_get_code); 25 mBtnGetCode.setOnClickListener(new OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 mTimeCount = null; 29 mTimeCount = new TimeCount(60 * 1000, 1000); 30 mTimeCount.start(); 31 } 32 }); 33 34 mBtnGetCode2 = (Button) findViewById(R.id.main_btn_get_code_2); 35 mBtnGetCode2.setOnClickListener(new OnClickListener() { 36 @Override 37 public void onClick(View v) { 38 mBtnGetCode2.setClickable(false); 39 mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.btn_unable)); 40 timeFlag = true; 41 new Thread() { 42 @Override 43 public void run() { 44 super.run(); 45 for (int i = 59; i >= 0 && timeFlag; i--) { 46 try { 47 sleep(1000); 48 Message msg = Message.obtain(); 49 msg.what = i; 50 mHandler.sendMessage(msg); 51 } catch (InterruptedException e) { 52 e.printStackTrace(); 53 } 54 } 55 } 56 }.start(); 57 } 58 }); 59 } 60 61 private Handler mHandler = new Handler() { 62 @Override 63 public void handleMessage(Message msg) { 64 super.handleMessage(msg); 65 if (msg.what > 0) { 66 mBtnGetCode2.setText("(" + msg.what + ")秒后重試"); 67 } else { 68 mBtnGetCode2.setText("獲取驗證碼"); 69 mBtnGetCode2.setClickable(true); 70 mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.colorAccent)); 71 } 72 } 73 74 75 }; 76 77 78 /** 79 * Activity 銷毀的時候注意把所有引用置為空,防止內存泄漏 80 */ 81 @Override 82 protected void onDestroy() { 83 super.onDestroy(); 84 mTimeCount = null; 85 timeFlag = false; 86 } 87 88 /** 89 * 實現倒計時的類 90 */ 91 private class TimeCount extends CountDownTimer { 92 93 /** 94 * @param millisInFuture The number of millis in the future from the call 95 * to {@link #start()} until the countdown is done and {@link #onFinish()} 96 * is called. 97 * @param countDownInterval The interval along the way to receive 98 * {@link #onTick(long)} callbacks. 99 */ 100 public TimeCount(long millisInFuture, long countDownInterval) { 101 super(millisInFuture, countDownInterval); 102 } 103 104 /** 105 * 計時過程顯示 按鈕不可用 設置為灰色 106 * 107 * @param millisUntilFinished 108 */ 109 @Override 110 public void onTick(long millisUntilFinished) { 111 mBtnGetCode.setClickable(false); 112 mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.btn_unable)); 113 mBtnGetCode.setText("(" + millisUntilFinished / 1000 + ")秒后重試"); 114 } 115 116 /** 117 * 計時結束調用 118 */ 119 @Override 120 public void onFinish() { 121 mBtnGetCode.setClickable(true); 122 mBtnGetCode.setText("獲取驗證碼方式1"); 123 mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); 124 } 125 } 126 127 128 }
簡單看一下xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context="com.example.nanchen.timerdemo.MainActivity"> 9 10 <Button 11 android:layout_marginTop="10dp" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:id="@+id/main_btn_get_code" 15 android:text="獲取驗證碼方式1" 16 android:background="@color/colorPrimaryDark"/> 17 18 <TextView 19 android:layout_width="match_parent" 20 android:layout_height="1dp" 21 android:id="@+id/main_line1" 22 android:background="@color/btn_unable" 23 android:layout_below="@+id/main_btn_get_code" 24 android:layout_marginTop="10dp"/> 25 26 <Button 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_below="@+id/main_line1" 30 android:layout_marginTop="10dp" 31 android:text="獲取驗證碼方式2" 32 android:id="@+id/main_btn_get_code_2" 33 android:background="@color/colorAccent"/> 34 35 </RelativeLayout>
寫在最后:雖然代碼和實現都非常簡單,你可能不費吹灰之力,不過倘若轉載的話,還是留個本文鏈接吧~thank you!
github鏈接:https://github.com/nanchen2251/TimerDemo
本文原創鏈接:http://www.cnblogs.com/liushilin/p/5951646.html