简体   繁体   中英

How can you make a stopwatch with the chronometer that shows milliseconds?

Hi I'm trying to make a scoreboard but I can't figure out how to get it to display milliseconds. In my xml a textview that displays ".000" and in my main java class I have

   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;

    }
}

}; }

elapsedRealtime() already is in milliseconds (specifically, " milliseconds since the system was booted, including deep sleep ").

If you want to display the elapsed time as seconds with a decimal point (eg, the TextView should show "2.198"), divide the calculated time difference by 1000. If you only want the milliseconds portion (eg, the TextView should show "198"), then use the modulo operator to get the remainder of division by 1000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM