如何在天文钟中设置开始,停止,重置?
我在我的应用中实现了计时器。我创建了三个按钮 启动,停止,重置 start:从00:00开始计时。 停止:完全停止计时器,显示运行时间。 reset:将计时器重置为00:00。
我正在尝试这样做,但是我的代码表现得很奇怪。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startButton(View view) {
((Chronometer) findViewById(R.id.chronometer)).start();
}
public void stopButton(View view) {
((Chronometer) findViewById(R.id.chronometer)).stop();
}
public void resetButton(View view) {
((Chronometer)
findViewById(R.id.chronometer)).setBase(SystemClock.elapsedRealtime());
}}
我该如何解决这个问题? 我只想实现一个开始按钮(从00:00开始),停止按钮(停止计时器并显示运行时间)和一个重置按钮(将计时器完全重置为00:00)。
答案 0 :(得分:0)
尝试
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
Chronometer simpleChronometer;
Button start, stop, restart, setFormat, clearFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate views
simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer);
start = (Button) findViewById(R.id.startButton);
stop = (Button) findViewById(R.id.stopButton);
restart = (Button) findViewById(R.id.restartButton);
setFormat = (Button) findViewById(R.id.setFormat);
clearFormat = (Button) findViewById(R.id.clearFormat);
// perform click event on start button to start a chronometer
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleChronometer.start();
}
});
// perform click event on stop button to stop the chronometer
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleChronometer.stop();
}
});
// perform click event on restart button to set the base time on chronometer
restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleChronometer.setBase(SystemClock.elapsedRealtime());
}
});
// perform click event on set Format button to set the format of chronometer
setFormat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleChronometer.setFormat("Time (%s)");
}
});
// perform click event on clear button to clear the current format of chronmeter as you set through set format
clearFormat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleChronometer.setFormat(null);
}
});
}
}
答案 1 :(得分:0)
我知道可能会晚一点,这可以在以后帮助某人
stop = (Button) findViewById(R.id.stopButton);
chronometer = findViewById(R.id.chronometerID);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.stop();
chronometer.setBase(SystemClock.elapsedRealtime());
}
});