在界面中实现时分秒倒计时
主要是将剩余时间转换为时分秒的字符串
public String secondConvertHourMinSecond(Long second) {
String str = "00:00:00";
if (second == null || second < 0) {
return str;
}
// 得到小时
long h = second / 3600;
str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";
// 得到分钟
long m = (second % 3600) / 60;
str += (m < 10 ? ("0" + m) : m) + ":";
//得到剩余秒
long s = second % 60;
str += (s < 10 ? ("0" + s) : s);
return str;
}
再启动一个Timer来刷新页面就行。
package com.adse.open.android.timedemo;
import android.os.Handler;
import android.os.Message;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;
public class TimerUtil {
private static final int nHandlerMsg = 23566124;
private int nStartDelayTime;
private int nIntervalTime;
private Timer mTimer;
private TimerTask mTimeTask;
private Handler mHandler;
private OnTimerChangeListener onTimerChangeListener;
public TimerUtil(int nStartDelayTime, int nIntervalTime, OnTimerChangeListener onTimerChangeListener) {
this.nStartDelayTime = nStartDelayTime;
this.nIntervalTime = nIntervalTime;
this.onTimerChangeListener = onTimerChangeListener;
}
/**
* 计时器开始
*/
public void timeStart() {
timeStop();
mHandler = new TimeHandler(this);
mTimeTask = new TimerTask() {
@Override
public void run() {
if (mHandler != null)
mHandler.obtainMessage(nHandlerMsg).sendToTarget();
}
};
mTimer = new Timer();
mTimer.schedule(mTimeTask, nStartDelayTime, nIntervalTime);
}
/**
* 停止计时器
*/
public void timeStop() {
if (mTimer != null)
mTimer.cancel();
if (mTimeTask != null)
mTimeTask.cancel();
mTimeTask = null;
mHandler = null;
mTimer = null;
}
private void doTimeChange() {
if (onTimerChangeListener != null)
onTimerChangeListener.doChange();
}
private static class TimeHandler extends Handler {
WeakReference<TimerUtil> weakReference;
public TimeHandler(TimerUtil timerUtil) {
weakReference = new WeakReference<>(timerUtil);
}
@Override
public void handleMessage(Message msg) {
TimerUtil timerUtil = weakReference.get();
if (msg.what == nHandlerMsg)
timerUtil.doTimeChange();
super.handleMessage(msg);
}
}
public interface OnTimerChangeListener {
void doChange();
}
public String secondConvertHourMinSecond(Long second) {
String str = "00:00:00";
if (second == null || second < 0) {
return str;
}
// 得到小时
long h = second / 3600;
str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";
// 得到分钟
long m = (second % 3600) / 60;
str += (m < 10 ? ("0" + m) : m) + ":";
//得到剩余秒
long s = second % 60;
str += (s < 10 ? ("0" + s) : s);
return str;
}
}
使用
package com.adse.open.android.timedemo;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import java.util.Observable;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class MainActivity extends AppCompatActivity {
private String TAG = "MainActivity";
private Context mContext;
private TextView mTvTime;
private Button mBtnStart;
private Button mBtnEnd;
private TimerUtil timerUtil;
private final long COUNT_TIME = 1*60;//计时总数,单位/秒
private long currConnTimes = COUNT_TIME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTimerUtil();
mContext = this;
mTvTime = findViewById(R.id.tv_time);
mBtnStart = findViewById(R.id.btn_start);
mBtnEnd = findViewById(R.id.btn_end);
mBtnStart.setOnClickListener(v -> {
timerUtil.timeStart();
});
mBtnEnd.setOnClickListener(v -> {
timerUtil.timeStop();
});
}
private void initTimerUtil() {
TimerUtil.OnTimerChangeListener onTimerChangeListener = new TimerUtil.OnTimerChangeListener() {
@Override
public void doChange() {
currConnTimes --;
if (currConnTimes <= 0){
currConnTimes = COUNT_TIME;
}
mTvTime.setText(timerUtil.secondConvertHourMinSecond(currConnTimes));
}
};
timerUtil = new TimerUtil(0, 1000, onTimerChangeListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
timerUtil.timeStop();
}
}