android中的倒計時功能,主要用於發送驗證碼以及其他。
我在這個項目裏用於打卡。
//讀卡倒計時邏輯
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
bt_get_cart_id.setText("點擊讀卡");
txtCode.setText("讀卡超時 請重試");
bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();//打卡用子線程
time.start();
}
});
ColseCom();
}
public void onTick(long millisUntilFinished) {
bt_get_cart_id.setText("讀卡中");
bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bt_get_cart_id.setText("點擊讀卡");
bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();//打卡用子線程
time.start();
}
});
ColseCom();
time.cancel();
}
});
}
}
代碼有點亂,只能作爲參考。
用法:
TimeCount time = new TimeCount(6000,1000);//倒計時六秒