格式秒表显示00:00 [英] Format Stopwatch to display 00:00

查看:1021
本文介绍了格式秒表显示00:00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序之一使用秒表尝试,我已经它得到那里将开始启动计数秒,停在停止计数。我的问题是,它会保持在60秒后继续。比如我得到120秒如果我等了两分钟。

I am experimenting with using a stopwatch in one of my apps and I have got it to where it will start counting seconds on start and stop counting on stop. My problem is that it will keep going on after 60 seconds. For example I get 120 seconds if I waited for two minutes.

所以我的问题是, 我怎么能做出这么一度达到60分钟,将增加一个和秒钟将开始在秒?

So my question is how can I make it so once the seconds reached 60 the minutes would be increased by one and the seconds would start over?

因此​​,而不是:120我会得到2:00。这里是code我有:

So instead of :120 I would get 2:00. Here is the code I have:

final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;

Stopwatch timer = new Stopwatch();
final int REFRESH_RATE = 100;

Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
        case MSG_START_TIMER:
            timer.start(); 
            mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
            break;

        case MSG_UPDATE_TIMER:              
            tvTextView.setText(":"+ timer.getElapsedTimeSecs());                
            mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); 
            break;                                  
        case MSG_STOP_TIMER:
            mHandler.removeMessages(MSG_UPDATE_TIMER); 
            timer.stop();
            tvTextView.setText("");
            break;

        default:
            break;
        }
    }
};

同样是:

 public void start(View v) {
    mHandler.sendEmptyMessage(MSG_START_TIMER);
}

public void stop(View v) {
    mHandler.sendEmptyMessage(MSG_STOP_TIMER);
}

顺便说一句我看着这个问题,但他的问题与时间跨度,如果我理解正确的是秒表(纠正我,如果我错了)的不同。感谢您的时间和精力。

By the way I looked at this question but his issue was with TimeSpan and if I understand correctly that is different from Stopwatch(correct me if I am wrong). Thanks for your time and effort.

推荐答案

如果你开始在几秒钟时间:

If you start with the time in seconds:

display = String.format("%d:%02d", seconds / 60, seconds % 60);

如果您不希望分钟,如果它们是0:

If you don't want the minutes if they are 0:

if (seconds < 60)
    display = String.format("%02d", seconds);
else
    display = String.format("%d:%02d", seconds / 60, seconds % 60);

这篇关于格式秒表显示00:00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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