自定义订单倒计时实现
工具类:
import android.annotation.SuppressLint;
import android.os.CountDownTimer;
/**
* Created by ptt on 2019/4/9.
* 倒计时
*/
public class TextViewTimeCountUtil extends CountDownTimer {
private OnRemindTimeEndListener mOnRemindTimeEndListener;
// 在这个构造方法里需要传入三个参数,一个是Activity,一个是总的时间millisInFuture,一个是countDownInterval,然后就是你在哪个按钮上做这个是,就把这个按钮传过来就可以了
public TextViewTimeCountUtil(long millisInFuture, long countDownInterval, OnRemindTimeEndListener mTimeEndListener) {
super(millisInFuture, countDownInterval);
this.mOnRemindTimeEndListener = mTimeEndListener;
}
@SuppressLint("NewApi")
@Override
public void onTick(long millisUntilFinished) {
if (null != mOnRemindTimeEndListener) {
mOnRemindTimeEndListener.onTick(millisUntilFinished);
}
}
@SuppressLint("NewApi")
@Override
public void onFinish() {
if (null != mOnRemindTimeEndListener) {
mOnRemindTimeEndListener.remindTimeEnd(1);
}
}
public interface OnRemindTimeEndListener {
void remindTimeEnd(int timeEnd);
void onTick(long time);
}
}
如何使用:
private TextViewTimeCountUtil tcu;
/**
* 设置剩余时间
*/
private void setRecLen(long orderTime, long nowTime) {
orderTime += 30 * 1000 * 60;
String orderEndTime = ConversionStringUtils.getFormatTime("yyyy-MM-dd HH:mm:ss", orderTime);
String nowTimeStr = ConversionStringUtils.getFormatTime("yyyy-MM-dd HH:mm:ss", nowTime);
Log.e("时间", orderEndTime + " / " + nowTimeStr);
long time = orderTime - nowTime;
if (time > 0) {
TextViewTimeCountUtil util = mHashMap.get(tvHourOne);
if (null == util) {
tcu = new TextViewTimeCountUtil(time, 1000, new TextViewTimeCountUtil.OnRemindTimeEndListener() {
@Override
public void remindTimeEnd(int timeEnd) {
if (timeEnd == 1) {
//倒计时已结束
isPay = 1;
tvHourOne.setText("0");
btnSubmit.setBackgroundResource(R.color.color_F7F7F7);
btnSubmit.setClickable(false);
}
}
@Override
public void onTick(final long time) {
new Thread() {
@Override
public void run() {
super.run();
runOnUiThread(new Runnable() {
@Override
public void run() {
final String[] endtime = ConversionStringUtils.getSecondToDayHourMinutes(time).split(":");
// LogUtils.eTag("endtime--:", ConversionStringUtils.getSecondToDayHourMinutes(time));
tvHourOne.setText(endtime[2].substring(0, 1));//分
tvHourTwo.setText(endtime[2].substring(1, 2));//分
tvMinuteOne.setText(endtime[3].substring(0, 1));//秒
tvMinuteTwo.setText(endtime[3].substring(1, 2));//秒
}
});
}
}.start();
}
});
//启动倒计时
tcu.start();
//保存当前到时任务
mHashMap.put(tvHourOne, tcu);
}
} else {
isPay = 1;
tvHourOne.setText("0");
btnSubmit.setBackgroundResource(R.color.color_F7F7F7);
btnSubmit.setClickable(false);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (tcu != null) {
tcu.cancel();
}
}
ConversionStringUtils里用到的方法:
/**
* 根据时间格式转换为时间戳
*/
public static String getFormatTime(String pattern, long date) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
Date date2 = new Date();
date2.setTime(date);
return sdf.format(date2);
}
/**
* 将毫秒数换算成x天x时x分x秒x毫秒
* time 毫秒
*/
public static String getSecondToDayHourMinutes(long ms) {
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
String strDay = day < 10 ? "0" + day : "" + day;
String strHour = hour < 10 ? "0" + hour : "" + hour;
String strMinute = minute < 10 ? "0" + minute : "" + minute;
String strSecond = second < 10 ? "0" + second : "" + second;
Log.e("时间", strDay + ":" + strHour + ":" + strMinute + ":" + strSecond);
return strDay + ":" + strHour + ":" + strMinute + ":" + strSecond;
}