我写了一个计时器代码。清除功能不清除时间值(当我按下清除仅文本更改,但时间值继续形成停止点)这里是代码,任何激活清除按钮的建议?:
public class MainActivity extends Activity {
private Button startButton;
private Button stopButton;
private Button clearButton;
private TextView timeValue;
private long timeStart = 0L;
private Handler timeHandler = new Handler();
long timeInMilisec = 0L;
long timeMemo = 0L;
long timeUpdate = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeValue = (TextView) findViewById(R.id.timeValue);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeStart = SystemClock.uptimeMillis();
timeHandler.postDelayed(updateTimerThread, 0);
}
});
stopButton = (Button) findViewById(R.id.stopButton);
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeMemo += timeInMilisec;
timeHandler.removeCallbacks(updateTimerThread);
}
});
clearButton = (Button) findViewById(R.id.clearButton);
clearButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.clearButton:
timeValue.setText("00:00:00");
int secs = 0;
int mins = 0;
secs = 0;
int milliseconds = 0;
timeInMilisec = SystemClock.uptimeMillis() - timeStart;
timeUpdate = timeMemo + timeInMilisec;
timeValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timeHandler.removeCallbacksAndMessages(updateTimerThread);
timeValue.setText("00:00:00");
break;
}
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilisec = SystemClock.uptimeMillis() - timeStart;
timeUpdate = timeMemo + timeInMilisec;
int secs = (int) (timeUpdate / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (timeUpdate % 1000);
timeValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timeHandler.postDelayed(this, 0);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
在ClearButton onClick中,您没有重置“timerstart”和“timeMemo”变量的值。这就是为什么只更新文本但没有重置计时器的原因。
答案 1 :(得分:0)
您应该重置 timeMemo 变量。
clearButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.clearButton:
// Clear the timestart and timeMemo
timeMemo = 0L;
timeValue.setText("00:00:00");
int secs = 0;
int mins = 0;
secs = 0;
int milliseconds = 0;
timeInMilisec = SystemClock.uptimeMillis() - timeStart;
timeUpdate = timeMemo + timeInMilisec;
timeValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
timeHandler.removeCallbacksAndMessages(updateTimerThread);
timeValue.setText("00:00:00");
timeHandler.removeCallbacks(updateTimerThread);
break;
}
}
});