int second = 0;
Handler handler = new Handler(); handler.post(runnable); Runnable runnable = new Runnable() { @Override public void run() { second++; patrol_time_txt.setText(Util.getTime(second)); handler.postDelayed(runnable, 1000); } };
//根據秒數轉化為時分秒 00:00:00
public static String getTime(int second) { if (second < 10) { return "00:0" + second; } if (second < 60) { return "00:" + second; } if (second < 3600) { int minute = second / 60; second = second - minute * 60; if (minute < 10) { if (second < 10) { return "0" + minute + ":0" + second; } return "0" + minute + ":" + second; } if (second < 10) { return minute + ":0" + second; } return minute + ":" + second; } int hour = second / 3600; int minute = (second - hour * 3600) / 60; second = second - hour * 3600 - minute * 60; if (hour < 10) { if (minute < 10) { if (second < 10) { return "0" + hour + ":0" + minute + ":0" + second; } return "0" + hour + ":0" + minute + ":" + second; } if (second < 10) { return "0" + hour + minute + ":0" + second; } return "0" + hour + minute + ":" + second; } if (minute < 10) { if (second < 10) { return hour + ":0" + minute + ":0" + second; } return hour + ":0" + minute + ":" + second; } if (second < 10) { return hour + minute + ":0" + second; } return hour + minute + ":" + second; }