[android]代码库
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//递减
time--;
//方法
String formatLongToTimeStr = formatLongToTimeStr(time);
//拆分0:时,1:分,2:秒
String[] split = formatLongToTimeStr.split(":");
for (int i = 0; i < split.length; i++) {
if(i==0){
tvtime1.setText(split[0]+"小时");
}
if(i==1){
tvtime2.setText(split[1]+"分钟");
}
if(i==2){
tvtime3.setText(split[2]+"秒");
}
}
//当time大于0时持续发送
if(time>0){
handler.postDelayed(this, 1000);
}
}
};
//时间转换方法
public String formatLongToTimeStr(int l) {
int hour = 0;
int minute = 0;
int second = 0;
second = l ;
//当秒大于60转换成分钟,零头转换成秒
if (second > 60) {
minute = second / 60; //取整
second = second % 60; //取余
}
//当分钟大于60转换成小时,零头转换成分钟
if (minute > 60) {
hour = minute / 60;
minute = minute % 60;
}
//拼接字符串
String strtime = hour+":"+minute+":"+second;
return strtime;
}