I was use chronometer to make a Stopwatch with this code
public class ChronoExample extends Activity {
Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
mChronometer = new Chronometer(this);
// Set the initial value
mChronometer.setText("00:00:00.000");
layout.addView(mChronometer);
Button startButton = new Button(this);
startButton.setText("Start");
startButton.setOnClickListener(mStartListener);
layout.addView(startButton);
Button stopButton = new Button(this);
stopButton.setText("Stop");
stopButton.setOnClickListener(mStopListener);
layout.addView(stopButton);
Button resetButton = new Button(this);
resetButton.setText("Reset");
resetButton.setOnClickListener(mResetListener);
layout.addView(resetButton);
setContentView(layout);
}
private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,
Toast.LENGTH_SHORT).show();
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 4) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
} else if (array.length == 5) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 24 * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 60 * 1000
+ Integer.parseInt(array[2]) * 60 * 1000
+ Integer.parseInt(array[3]) * 1000;
}
mChronometer.setBase(SystemClock.elapsedRealtimeNanos() - stoppedMilliseconds);
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
showElapsedTime();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
showElapsedTime();
}
};
I'm not sure with my code here to declare the format to "HH:MM:SS.SSS" in this line, maybe someone can help me fix this
int stoppedMilliseconds = 0;
String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 4) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
} else if (array.length == 5) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 24 * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 60 * 1000
+ Integer.parseInt(array[2]) * 60 * 1000
+ Integer.parseInt(array[3]) * 1000;
}
mChronometer.setBase(SystemClock.elapsedRealtimeNanos() - stoppedMilliseconds);
mChronometer.start();
I purpose to make it Show "hh:mm:ss.SSS" but in emulator it seems bug like this
Chronometer not count time but count something like + ( ) '
Anyone can help me to fix this? thanks-