请任何人告诉我如何启动计时器以及如何停止
答案 0 :(得分:0)
Here您找到了所有必要的信息。如您所见,有多种方法可以启动计时器。只需使用适当的schedule(...)
方法即可。为了停止计时器,您可以使用cancel()方法,然后使用purge()方法。但请注意,cancel()方法会取消与此计时器关联的所有任务。
答案 1 :(得分:0)
你在谈论Timer
班吗?
您可以使用schedule
方法安排TimerTask
和TimerTask#cancel
取消它。
答案 2 :(得分:0)
如果你想停止倒数计时器..
Timer.cancel();
答案 3 :(得分:0)
Use this Working Code.
package com.example.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class StopWatch extends Activity {
Chronometer mChronometer;
Button button;
private long start = SystemClock.elapsedRealtime();
private long stop = 0;
private long totalPauseTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
final Chronometer chrono = (Chronometer) findViewById(R.id.chronometer1);
final ToggleButton toggle2 = (ToggleButton) findViewById(R.id.toggleButton2);
final Chronometer chrono2 = (Chronometer) findViewById(R.id.chronometer2);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
try {
Log.i("[chrono]", String.format("start:%d stop:%d delta: %d",start, stop, start - stop));
Log.i("[chrono]", "base:" +chrono.getBase());
Log.i("[chrono]", "elapsed time:" +SystemClock.elapsedRealtime());
if (isChecked) {
chrono.start();
start = SystemClock.elapsedRealtime();
chrono.setBase(start - totalPauseTime);
} else {
totalPauseTime += SystemClock.elapsedRealtime() - start;
chrono.stop();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
toggle2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
try {
Log.i("[chrono]", String.format("start:%d stop:%d delta: %d",start, stop, start - stop));
Log.i("[chrono]", "base:" +chrono.getBase());
Log.i("[chrono]", "elapsed time:" +SystemClock.elapsedRealtime());
if (isChecked) {
chrono2.start();
start = SystemClock.elapsedRealtime();
chrono2.setBase(start - totalPauseTime);
} else {
totalPauseTime += SystemClock.elapsedRealtime() - start;
chrono2.stop();
}
} catch (Exception e) {
enter code here
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }