同时显示天小时分秒使用倒计时在android系统计时器倒计时的问题 [英] Countdown issue while displaying days hours minutes seconds using count down timer in android
问题描述
我使用倒数计时器在我的应用程序,我需要几天时间分秒left.I得到了天,小时,分钟和秒来显示即将到来的日期,但是当我将其设置为文本浏览倒计时不启动下面.The是我的code。
日期日期=新Date(2013,Integer.parseInt(datess.get(k).split(\"-\")[1])-1,Integer.parseInt(datess.get(k).split(\"-\")[0]),hours,mins,secs);
长dtMili = System.currentTimeMillis的();
日期dateNow =新的日期(dtMili);
留= date.getTime() - dateNow.getTime();
mycount的计数器=新mycount的(保持,1000);
counter.start();公共类mycount的扩展CountDownTimer {
公共mycount的(长millisInFuture,长countDownInterval){
超(millisInFuture,countDownInterval);
} @覆盖
公共无效onFinish(){
// TODO自动生成方法存根 tv3.setText(完成);
} @覆盖
公共无效onTick(长millisUntilFinished){
// TODO自动生成方法存根
tv3.setText(timeCalculate(millisUntilFinished / 1000)+倒计时); }
} 公共字符串timeCalculate(长TTIME)
{
长daysuuu,hoursuuu,minutesuuu,secondsuuu;
串daysT =,restT =; daysuuu =(Math.round(TTIME)/ 86400);
hoursuuu =(Math.round(TTIME)/ 3600) - (daysuuu * 24);
minutesuuu =(Math.round(TTIME)/ 60) - (daysuuu * 1440) - (hoursuuu * 60);
secondsuuu = Math.round(TTIME)%60;
如果(daysuuu == 1)daysT =的String.format(%d日,daysuuu);
如果(daysuuu→1)daysT =的String.format(%d天,daysuuu); restT =的String.format(%02D:%02D:%02D,hoursuuu,minutesuuu,secondsuuu); 返回daysT + restT;
}
这是输出
为什么不倒计时开始了吗?任何建议都AP preciated。
您没有使用 millisUntilFinished
参数来更新你的 timeCalculate时间( )
。首先:
@覆盖
公共无效onTick(长millisUntilFinished){
tv3.setText(millisUntilFinished +倒计时);
}
一旦你确认计时器工作,你需要一种方法来 millisUntilFinished
转换为人类可读的字符串。
有关日期和时间Java中的类是不必要难以处理,并有一些模糊的错误。 (例如多Date类是德precated,但推荐Calendar类仍然依赖于大量的日期......)的第三方库的乔达时间是一种流行的替代品。
加入
输出不是我所期待的。
块引用>25000至700天在未来是不是我所期望的不是,
日期
似乎是错误的。加布里埃尔指出了另一个答案对这个问题的年份是从1900年虽然别的计算仍然是自25000错误天低于70年的未来......结果
然而,这种构造方法的德precated 的,不应该被使用,日历是建议类。我写了一个快速演示使用日历为您服务。但相信CountDownTimer本身有一对夫妇的根本缺陷:
- 每次
onTick()
被称为CDT增加了几毫秒的总时间,我发现它可以轻松地添加,每天7分半钟。- 因为第一个错误的最后一个
onTick()
可能不被调用。当从5倒计时,我通常会看到5,4,3,2,<长暂停>中
然后onFinished()
显示0
...我在previous问题改写CDT:机器人CountDownTimer - 额外的延迟毫秒蜱之间。
I am using countdown timer in my application where i need to display upcoming date in days hours minutes and seconds left.I got the days,hours,minutes and seconds but when i set it to text view the countdown doesn't start.The below is my code.
Date date = new Date(2013,Integer.parseInt(datess.get(k).split("-")[1])-1,Integer.parseInt(datess.get(k).split("-")[0]),hours,mins,secs); long dtMili = System.currentTimeMillis(); Date dateNow = new Date(dtMili); remain = date.getTime() - dateNow.getTime(); MyCount counter = new MyCount(remain,1000); counter.start(); public class MyCount extends CountDownTimer{ public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { // TODO Auto-generated method stub tv3.setText("done"); } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub tv3.setText(timeCalculate(millisUntilFinished/1000) + " Countdown"); } } public String timeCalculate(long ttime) { long daysuuu,hoursuuu, minutesuuu, secondsuuu; String daysT = "", restT = ""; daysuuu = (Math.round(ttime) / 86400); hoursuuu = (Math.round(ttime) / 3600) - (daysuuu * 24); minutesuuu = (Math.round(ttime) / 60) - (daysuuu * 1440) - (hoursuuu * 60); secondsuuu = Math.round(ttime) % 60; if(daysuuu==1) daysT = String.format("%d day ", daysuuu); if(daysuuu>1) daysT = String.format("%d days ", daysuuu); restT = String.format("%02d:%02d:%02d", hoursuuu, minutesuuu, secondsuuu); return daysT + restT; }
This is the output
Why is the countdown not started? Any Suggestions are appreciated.
解决方案You are not using the
millisUntilFinished
parameter to update your time intimeCalculate()
. Start with:@Override public void onTick(long millisUntilFinished) { tv3.setText(millisUntilFinished + " Countdown"); }
Once you've confirmed that the timer is working, you'll need a method to convert
millisUntilFinished
into a human readable String.The classes related to dates and times in Java are unnecessarily difficult to work with and have some obscure errors. (For instance much of the Date class is deprecated, but the recommended Calendar class still relies on Date heavily...) The 3rd party library Joda Time is a popular replacement.
Addition
the output is not what i expected
25,000 to 700 days in the future is not what I would expect either,
date
appears to be wrong. As gabriel points out in another answer to this question the year value is calculated from 1900. Though something else is still wrong since 25,000 days is less than 70 years in the future...
However this constructor is deprecated and shouldn't be used, Calendar is the recommended class. I wrote a quick demo using Calendar for you.But understand that CountDownTimer itself has a couple fundamental flaws:
- Every time
onTick()
is called CDT adds a few milliseconds to the overall time, I noticed that it can easily add 7 and half minutes every day.- Because of the first error the last
onTick()
might no be called. When counting down from 5, I typically see"5, 4, 3, 2, <long pause>"
thenonFinished()
displays"0"
...I rewrote CDT in a previous question: android CountDownTimer - additional milliseconds delay between ticks.
这篇关于同时显示天小时分秒使用倒计时在android系统计时器倒计时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!