Java SystemClock.elapsedRealtimeNanos()不工作
我用这个密码用计时器做了一个秒表Java SystemClock.elapsedRealtimeNanos()不工作,java,android,arrays,real-time,chronometer,Java,Android,Arrays,Real Time,Chronometer,我用这个密码用计时器做了一个秒表 public class ChronoExample extends Activity { Chronometer mChronometer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this);
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();
}
};
我不确定我的代码是否可以在这行中将格式声明为“HH:MM:SS.SSS”,也许有人可以帮我解决这个问题
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();
我打算让它显示“hh:mm:ss.SSS”,但在emulator中,它似乎是这样的错误
计时器不计算时间,而是像+()一样计算时间
有人能帮我解决这个问题吗?谢谢-