不确定如何重置暂停的秒表
Not sure how to reset my paused stopwatch
(我对编码还很陌生)好吧,我已经找到了我以前的帖子的解决方案并想出了如何显示毫秒......但是,我似乎无法让暂停的实例重置回来至 00:00:00
我已经尝试了几种方法,例如 mreset elapsedTime 和将 base 设置为 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);
}
};
}
如有任何帮助,我们将不胜感激!
在您的 resetButton 操作中尝试将所有值设置回 0
在重置按钮部分,您需要包含一个 onClick 事件并将时间交换设置为 0。像这样:
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
finalTime = 0;
timeInMillis = 0L;
timeSwap = 0L;
(我对编码还很陌生)好吧,我已经找到了我以前的帖子的解决方案并想出了如何显示毫秒......但是,我似乎无法让暂停的实例重置回来至 00:00:00
我已经尝试了几种方法,例如 mreset elapsedTime 和将 base 设置为 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);
}
};
}
如有任何帮助,我们将不胜感激!
在您的 resetButton 操作中尝试将所有值设置回 0
在重置按钮部分,您需要包含一个 onClick 事件并将时间交换设置为 0。像这样:
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
finalTime = 0;
timeInMillis = 0L;
timeSwap = 0L;