天天看点

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>