下面是我在工作中用的一个关于倒计时的工具类,直接复制的,再不写了,,,,用的话直接粘!!!!
(1): 这是工具类
<span style="font-family:Courier New;font-size:14px;">package com.exception.android.wk.seller.ui.widget;
import android.os.CountDownTimer;
import android.text.format.DateUtils;
import android.widget.Button;
import com.exception.android.wk.seller.R;
public class CaptchaTimer extends CountDownTimer
{
private Button button;
public CaptchaTimer(long millisInFuture, long countDownInterval, Button button)
{
super(millisInFuture, countDownInterval);
this.button = button;
}
public CaptchaTimer(long millisInFuture, long countDownInterval)
{
this(millisInFuture, countDownInterval, null);
}
public void startTimer()
{
if (button != null)
{
button.setEnabled(false);
}
start();
}
@Override
public void onTick(long millisUntilFinished)
{
if (button == null)
{
return;
}
button.setText(millisUntilFinished / DateUtils.SECOND_IN_MILLIS + "");
}
@Override
public void onFinish()
{
if (button == null)
{
return;
}
button.setText(R.string.public_captcha_button);
button.setEnabled(true);
}
}
</span>
首先创建这个类的对象,在创建对象的时候要传递三个对象(第一个参数是设置的总时长,一般为60s,,第二个参数是两时间间的间距,一般为1秒,第三是你设置给的控件)
CaptchaTimer captchaTimer;
captchaTimer = new CaptchaTimer(DateUtils.MINUTE_IN_MILLIS, DateUtils.SECOND_IN_MILLIS, captchaView.getCaptchaCountDownView());
然后在你需要的地方调用此方法:
captchaTimer.startTimer();
希望能对你有用!!!!!!!!!!!!!!!!!!!!!