【问题标题】:How can you make a stopwatch with the chronometer that shows milliseconds?如何用显示毫秒的计时器制作秒表?
【发布时间】:2012-10-28 17:48:42
【问题描述】:

您好,我正在尝试制作记分牌,但我不知道如何让它显示毫秒。在我的 xml 中显示“.000”的文本视图,在我的主要 java 类中我有

   public class MainActivity extends Activity {
   timeEx = (TextView) findViewById(R.id.timeEx);
ending= 000;

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {

    if (running == true){


    }else{

    MyChronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
    MyChronometer.start();
    running = true;
     TimeEnd();

   }

    }
}

private void TimeEnd() {
    // TODO Auto-generated method stub
    while(running == true){

        ending ++;
        timeEx.setText("."+ ending );
        if (ending == 999)
            ending = ending - 999;

    }
}

}; }

【问题讨论】:

  • 请发布一个简短的、缩进的、完整的、可运行的程序来突出您的问题。

标签: java android stopwatch milliseconds chronometer


【解决方案1】:

elapsedRealtime() 已经以毫秒为单位(特别是“milliseconds since the system was booted, including deep sleep”)。

如果您想将经过的时间显示为带小数点的秒数(例如,TextView 应该显示“2.198”),请将计算出的时间差除以 1000。如果您只想要毫秒部分(例如, TextView 应该显示“198”),然后使用模运算符得到除以 1000 的余数。

【讨论】:

    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2015-04-17
    • 2015-10-22
    • 1970-01-01
    相关资源
    最近更新 更多