我们如何在不使用计时器类且仅使用“SystemClock”类的情况下编写类似于以下(具有相同功能)的秒表计时器?到目前为止,我看到的大多数示例要么只是使用计时器类,要么是两者的组合。感谢您的提示:
package com.example.stopwatch;
import com.example.stopwatch.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends Activity implements OnClickListener {
private Button start;
private Button stop;
private Button reset;
private Chronometer mydChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uI();
}
public void uI() {
start = (Button) findViewById(R.id.btstart);
start.setOnClickListener(this);
stop = (Button) findViewById(R.id.btstop);
stop.setOnClickListener(this);
reset = (Button) findViewById(R.id.btreset);
reset.setOnClickListener(this);
mydChronometer = (Chronometer) findViewById(R.id.chronometer1);
}
@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;
}
@Override
public void onClick(View v) {
if (v == start) {
mydChronometer.start();
} else if (v == stop) {
mydChronometer.stop();
} else if (v== reset) {
mydChronometer.setBase(SystemClock.elapsedRealtime());
}
}
}