基本上,我有一个Stopwatch应用程序,该应用程序当前具有启动计时器,停止所述计时器并记录在按下记录按钮时暂停的当前计时器值时读取的数据的功能。
例如:
问题:无论何时计时器停止,它都会在经过的时间暂停,而不是重置为零并保持暂停状态
我已经尝试将开始按钮中的代码并入,以便在记录数据后立即将计时器值重置为零;但是,这会导致在按下“记录”按钮后立即开始下一次试用。
long startTime=0L,timeinMilliseconds=0L,timeSwapBuff=0L,updateTime=0L;
Runnable updateTimerThread = new Runnable() {
@Override
public void run() {
timeinMilliseconds = SystemClock.uptimeMillis() - startTime;
updateTime = timeSwapBuff + timeinMilliseconds;
int secs = (int) (updateTime / 1000);
int mins = secs / 60;
secs %= 60;
int milliseconds = (int) (updateTime % 1000);
txtTimer.setText("" + mins +":"+ String.format("%2d", secs) + ":" + String.format("%3d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
btnStart.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
TextView txtValue = (TextView) time.findViewById(R.id.txtContent);
txtValue.setText(txtTimer.getText());
String timerString = txtTimer.getText().toString();
stringNums.add(timerString); //adds string timer value to an array
customHandler.removeCallbacks(updateTimerThread); //pauses timer
//startTime = SystemClock.uptimeMillis();
//customHandler.postDelayed(updateTimerThread,0);
//Restarts timer to zero but immediately begins next trial
}