第一种自定义一个TimeCount
public class TimeCount extends CountDownTimer {
private Button button;
//参数依次为总时长,和计时的时间间隔
public TimeCount(Button button, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.button = button;
}
//计时过程显示
@Override
public void onTick(long millisUntilFinished) {
String time = "(" + millisUntilFinished / 1000 + ")秒";
setButtonInfo(time, "#c1c1c1", false);
}
//计时完毕时触发
@Override
public void onFinish() {
setButtonInfo("重新获取", "#f95353", true);
}
/**
* 验证按钮在点击前后相关设置
*
* @param content 要显示的内容
* @param color 颜色值
* @param isClick 是否可点击
*/
private void setButtonInfo(String content, String color, boolean isClick) {
button.setText(content);
button.setBackgroundColor(Color.parseColor(color));
button.setClickable(isClick);
}
}
private TimeCount timeCount;
@Override
protected void onResume() {
super.onResume();
timeCount = new TimeCount(btn1, 10000, 1000);
}
private void timer1() {
timeCount.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
timeCount.cancel();
cancel();
}
第二种使用Handler
/**
* 第二种方式:使用Handler
*
* 静态内部类:避免内存泄漏
*/
private static class MyHandler extends Handler {
private final WeakReference weakReference;
public MyHandler(MainActivity activity) {
weakReference = new WeakReference(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity activity = weakReference.get();
if (activity != null) {
switch (msg.what) {
case 0:
if (msg.arg1 == 0) {
btn2.setText("重新获取");
btn2.setBackgroundColor(Color.parseColor("#f95353"));
btn2.setClickable(true);
} else {
btn2.setText("(" + msg.arg1 + ")秒");
btn2.setBackgroundColor(Color.parseColor("#c1c1c1"));
btn2.setClickable(false);
}
break;
}
}
}
}
/**
* 监听按钮下直接调用即可
*/
private void sendMessageClick() {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 9; i >= 0; i--) {
Message msg = myHandler.obtainMessage();
msg.arg1 = i;
myHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
第三种使用Rxjava
/**
* 每隔milliseconds毫秒后执行next操作
*
* @param seconds
*/
private void startCount(long seconds) {
Observable.interval(0, seconds, TimeUnit.SECONDS)
.map(new Function() {
@Override
public Long apply(Long aLong) throws Exception {
return 10 - aLong.longValue();
}
})
.take(11)//设置循环次数
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
mDisposable = disposable;
}
@Override
public void onNext(@NonNull Long number) {//从0开始
btn3.setText("(" + number + ")秒");
btn3.setBackgroundColor(Color.parseColor("#c1c1c1"));
btn3.setClickable(false);
}
@Override
public void onError(@NonNull Throwable e) {
cancel();
}
@Override
public void onComplete() {
btn3.setText("重新获取");
btn3.setBackgroundColor(Color.parseColor("#f95353"));
btn3.setClickable(true);
}
});
}
第四种:
Flowable.intervalRange(0, remain, 0, 1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
long left = remain - aLong;
long hh = left / 3600;
long mm = (left % 3600) / 60;
long ss = (left % 3600) % 60;
String time = topTitle + " "
+ String.format("%02d", hh) + ":"
+ String.format("%02d", mm) + ":"
+ String.format("%02d", ss);
bd.tvTimer.setText(time);
}
})
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
//Log.d(TAG, "倒计时完毕");
}
})
.subscribe();