(我对编码很新)嗯,我遇到了以前帖子的解决方案,并想出了如何显示毫秒...但是,我似乎无法让暂停的实例重置回00 :00:00
我尝试了一些方法,例如mreset elapsedTime并将基数设置为00:00:00,但似乎没有任何效果!有什么建议吗?
这是Java文件:
package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button startButton;
private Button pauseButton;
private Button resetButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
private TextView textTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTimer = (TextView) findViewById(R.id.textTimer);
startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
});
pauseButton = (Button) findViewById(R.id.stop);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
});
//Updated.... Still having errors with parenthesis
resetButton = (Button) findViewById(R.id.reset);
resetButton.setOnClickListener(SystemClock.setCurrentTimeMillis()
timeInMillies = 0L);
timeSwap = 0L;
finalTime = 0L;
}
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
}
任何帮助将不胜感激!
答案 0 :(得分:0)
在您的resetButton操作中尝试将所有值设置回0
答案 1 :(得分:0)
在重置按钮部分,您需要包含一个onClick事件,并将时间交换设置为0。像这样:
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
finalTime = 0;
timeInMillis = 0L;
timeSwap = 0L;