倒数计时器在安卓 [英] countdown timer in android

查看:98
本文介绍了倒数计时器在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行一个倒计时时间,在此我想说明天,小时,秒,milisec剩余的具体日期。并且将是不断变化直到特定日期的结束。 希望你能理解。

i want to run an countdown time , in which i want to show days,hours,sec and milisec remaining for a specific date. and will be be keep changing till the end of the specific date. Hope you can understand.

在此先感谢。

推荐答案

嗯,我认为这个问题是,你不知道,如何用时间来工作。在这里,我有我用来计算的一些项目,我分析出一个数据库的时间量的方法。 该参数是一个双精度值,这已得到了以秒为单​​位的全部时间。它返回一个字符串,在日,时,分,秒为字符串的时间。

Well, I think the problem is, that you dont know, how to work with the time. Here i have a method I use to calculate the amount of time of some items which I parse out of a db. The param is a double value, which has got the whole time in seconds. It returns a string with the time in days, hours, minutes and seconds as string.

public String timeCalculate(double ttime) {

    long days, hours, minutes, seconds;
    String daysT = "", restT = "";

    days = (Math.round(ttime) / 86400);
    hours = (Math.round(ttime) / 3600) - (days * 24);
    minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
    seconds = Math.round(ttime) % 60;

    if(days==1) daysT = String.format("%d day ", days);
    if(days>1) daysT = String.format("%d days ", days);

    restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);

    return daysT + restT;
}

有关倒计时本身......把目标时间戳减去实际之一,瞧,你已经有了秒的时候:)把那些秒,以这种方式和你有剩余的时间。现在,你只需要做一些UI的东西;) 哦,和普通的Unix时间戳可以用这个小方法:

For the countdown itself...take the target timestamp minus the actual one and voila, you've got seconds left :) Put those seconds to this method and you've got the remaining time. Now you just need to do some UI things ;) Oh, and for the usual Unix Timestamp you can use this little method:

public static long getTimestamp() {
    return System.currentTimeMillis() / 1000;
}

这篇关于倒数计时器在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆