我有两个按钮“开始”和“暂停”和一个 Textview。
我遇到的问题是当我按下暂停按钮然后按下开始按钮时,秒表不会从停止的地方开始。
谢谢您的帮助
public class StopWatch extends Activity {
/** Called when the activity is first created. */
private TextView timeflg;
private Button startButton;
private Button pauseButton;
private long mStartTime = 0L;
private Handler mHandler = new Handler();
long millis = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (mStartTime == 0L) {
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 0);
}
timeflg = (TextView) findViewById(R.id.timeflag);
startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
mHandler.postDelayed(mUpdateTimeTask, 0);
}
});
pauseButton = (Button) findViewById(R.id.pause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
mHandler.removeCallbacks(mUpdateTimeTask);
}
});
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
millis = SystemClock.uptimeMillis() - mStartTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (millis%1000);
timeflg.setText("" + minutes + ":" + String.format("%02d",seconds)+ ":"
+ String.format("%03d",milliseconds));
mHandler.postDelayed(this,0);
}
};
}