[英]Display timer in textview contain days, hours, minutes and seconds in android
I am using countdown timer to show the left time in text view.It is working fine. 我正在使用倒计时器在文本视图中显示剩余时间。它工作正常。 Below is the code:-\\
下面是代码: - \\
public class MyCount extends CountDownTimer {
Context mContext;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onTick (long millisUntilFinished) {
MainActivity.timeDisplay.setText(formatTime(millisUntilFinished));
}
public void onFinish() {
AutomaticCallRecorderStaticMethod.startRecording();
}
}
public String formatTime(long millis) {
output = "";
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours=minutes/ 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours=hours%60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD=String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD+" : "+minutesD + " : " + secondsD;
return output;
}
Code to start the timer:- 启动计时器的代码: -
counter = new MyCount (length, 10);
Everything is working fine. 一切都很好。 It shows the timer in text view like as 00:00:00 and the maximum limit is 59:59:59.
它在文本视图中显示计时器,如00:00:00,最大限制为59:59:59。 It means only 60 hours.
这意味着只有60个小时。 Now I want that the limit should be increased if the user select 3 days then the timer should start from 71:59:59 or something like that so that the finish() method of countdown timer should be called after 3 days.How can I do that??
现在我希望限制应该增加,如果用户选择3天然后计时器应该从71:59:59或类似的东西开始,以便倒数计时器的finish()方法应该在3天后调用。我怎么能去做?? Beside it I want to show number of days also in the below format:-
除此之外,我想以下列格式显示天数: -
Days:Hours:minutes:seconds left . 天:小时:分钟:剩余秒数。
How can I achieve that.Please help me to sort out this problem. 我怎样才能做到这一点。请帮我解决这个问题。
I think, you can check the value, if(millisec > 24* 60*60*60*10... ) if so, divide by 24 to get number of days.. 我想,您可以查看值,如果(毫秒> 24 * 60 * 60 * 60 * 10 ...)如果是,则除以24得到天数。
(diff / (1000 * 60 * 60 * 24)); (diff /(1000 * 60 * 60 * 24)); //for counting days
//用于计算天数
or int days = (int) (milliseconds / (1000*60*60*24)); 或int days =(int)(毫秒/(1000 * 60 * 60 * 24));
Other method is, You can map with calender object I think.. I am not sure.. just try it out.. 其他方法是,你可以用日历对象映射我认为..我不确定..只是尝试一下..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.