一、效果图
二、思路
算多少秒,秒数取余60,(满足分后剩下的秒数)
算多少分,秒数除60,再取余60 (总分数满足小时后剩下的分数)
算多少时,秒数除60,除60,再取余24 (总小时满足天后剩下的小时)
算多少天,秒数除60,除60,除24 等到的整数就是天数
三、完整代码:
public static void liveDescCountTime(long ms,TextView tvDays, TextView tvHour,TextView tvMinutes,TextView tvSeconds){
long totalSeconds = ms / 1000;
long seconds = totalSeconds % 60;
long minutes = totalSeconds / 60 % 60;
long hours = totalSeconds / 60 / 60 % 24;
long days = totalSeconds / 60 / 60 / 24;
String dayStr = "";
if (days > 0){
if (days > 9) {
dayStr += days + "";
} else if (days > 0) {
dayStr += "0" + days + "";
} else {
dayStr += "00";
}
} else {
dayStr = "00";
}
tvDays.setText(dayStr);
String hourStr = "";
if (hours > 0){
if (hours > 9) {
hourStr += hours + "";
} else if (hours > 0) {
hourStr += "0" + hours + "";
} else {
hourStr += "00";
}
} else {
hourStr = "00";
}
tvHour.setText(hourStr);
String minutesStr = "";
if (minutes > 9) {
minutesStr += minutes + "";
} else if (minutes > 0) {
minutesStr += "0" + minutes + "";
} else {
minutesStr += "00";
}
tvMinutes.setText(minutesStr);
String secondStr = "";
if (seconds > 9) {
secondStr += seconds;
} else if (seconds > 0) {
secondStr += "0" + seconds;
} else {
secondStr += "00";
}
tvSeconds.setText(secondStr);
}
开始倒计时
Observable.interval(1,TimeUnit.MILLISECONDS)
.take(saveTime)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object :Observer<Long>{
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
countTimeDisposable = d
}
override fun onNext(aLong: Long) {
TimeUtils.liveDescCountTime(Math.abs(aLong - saveTime),tv_8,tv_2,tv_4,tv_6)
}
override fun onError(e: Throwable) {
}
})
注:saveTime就是你的毫秒数