天天看點

CountDownTimer 倒計時器的使用

<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">TextView不斷顯示剩下的時間,代碼如下:</span>
           
private TextView tv_Time;
	private CountDownTimer timer = new CountDownTimer(60000, 1000) {


		@Override
		public void onTick(long millisUntilFinished) {
		    <span style="font-family: Arial, Helvetica, sans-serif;">tv_Time</span>.setText((millisUntilFinished / 1000) + "秒後可重發");
		}


		@Override
		public void onFinish() {
			<pre name="code" class="java"><span style="white-space:pre">		</span>     tv_Time<span style="font-family: Arial, Helvetica, sans-serif;">.setEnabled(true);</span>
           

tv_Time.setText("擷取驗證碼");}};

<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">         timer.start();//調用</span>
           
<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">說明:CountDownTimer timer = new CountDownTimer(60000, 1000)中,第一個參數表示總時間,第二個參數表示間隔時間。意思就是每隔一秒會回調一次onTick,然後60秒之後會回調onFinish方法。</span>
</span>