Android基础之CountDownTimer 倒计时类

时间:2021-04-10 04:42:46

app常用的60s倒计时计时功能:

private static final int TIME_LIMIT = 60;
    private void initView() {
// 相关控件
mResend = (TextView) findViewById(R.id.resend); // 重新发送btn
mResend.setOnClickListener(this);
mTimeLimit = (TextView) findViewById(R.id.time_limit); // 倒计时秒数显示
countDown(); // 开始倒计时
}

利用android的倒计时类CountDownTimer:

private void countDown() {
mResend.setActivated(false);
mResend.setTextColor(getResources().getColor(R.color.trans_black_26)); new CountDownTimer(TIME_LIMIT * 1000, 1000) { @Override
public void onTick(long millisUntilFinished) {
mTimeLimit.setVisibility(View.VISIBLE);
mTimeLimit.setText(--reduceLen + "秒");// 实时显示秒数递减
} @Override
public void onFinish() {
mTimeLimit.setVisibility(View.INVISIBLE);
mResend.setActivated(true);
mResend.setTextColor(getResources().getColor(R.color.trans_black_54));
}
}.start();
}