package org.geeksforgeeks.stopwatch;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.os.Bundle;
import java.util.Locale;
import android.widget.TextView;
public class StopwatchActivity extends Activity {
// Use seconds, running and wasRunning respectively
// to record the number of seconds passed,
// whether the stopwatch is running and
// whether the stopwatch was running
// before the activity was paused.
// Number of seconds displayed
// on the stopwatch.
private int seconds = 0;
// Is the stopwatch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null) {
// Get the previous state of the stopwatch
// if the activity has been
// destroyed and recreated.
seconds
= savedInstanceState
.getInt("seconds");
running
= savedInstanceState
.getBoolean("running");
wasRunning
= savedInstanceState
.getBoolean("wasRunning");
}
runTimer();
}
// Save the state of the stopwatch
// if it's about to be destroyed.
@Override
public void onSaveInstanceState(
Bundle savedInstanceState)
{
savedInstanceState
.putInt("seconds", seconds);
savedInstanceState
.putBoolean("running", running);
savedInstanceState
.putBoolean("wasRunning", wasRunning);
}
// If the activity is paused,
// stop the stopwatch.
@Override
protected void onPause()
{
super.onPause();
wasRunning = running;
running = false;
}
// If the activity is resumed,
// start the stopwatch
// again if it was running previously.
@Override
protected void onResume()
{
super.onResume();
if (wasRunning) {
running = true;
}
}
// Start the stopwatch running
// when the Start button is clicked.
// Below method gets called
// when the Start button is clicked.
public void onClickStart(View view)
{
running = true;
}
// Stop the stopwatch running
// when the Stop button is clicked.
// Below method gets called
// when the Stop button is clicked.
public void onClickStop(View view)
{
running = false;
}
// Reset the stopwatch when
// the Reset button is clicked.
// Below method gets called
// when the Reset button is clicked.
public void onClickReset(View view)
{
running = false;
seconds = 0;
}
// Sets the NUmber of seconds on the timer.
// The runTimer() method uses a Handler
// to increment the seconds and
// update the text view.
private void runTimer()
{
// Get the text view.
final TextView timeView
= (TextView)findViewById(
R.id.time_view);
// Creates a new Handler
final Handler handler
= new Handler();
// Call the post() method,
// passing in a new Runnable.
// The post() method processes
// code without a delay,
// so the code in the Runnable
// will run almost immediately.
handler.post(new Runnable() {
@Override
public void run()
{
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
// Format the seconds into hours, minutes,
// and seconds.
String time
= String
.format(Locale.getDefault(),
"%d:%02d:%02d", hours,
minutes, secs);
// Set the text view text.
timeView.setText(time);
// If running is true, increment the
// seconds variable.
if (running) {
seconds++;
}
// Post the code again
// with a delay of 1 second.
handler.postDelayed(this, 1000);
}
});
}
}
更新秒表布局代码 这是布局的 XML。它描述了用于显示计时器的单个文本视图,以及用于控制秒表的三个按钮。将activity_stopwatch.xml中当前的 XML 替换为此处显示的 XML:
package org.geeksforgeeks.stopwatch;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.os.Bundle;
import java.util.Locale;
import android.widget.TextView;
public class StopwatchActivity extends Activity {
// Use seconds, running and wasRunning respectively
// to record the number of seconds passed,
// whether the stopwatch is running and
// whether the stopwatch was running
// before the activity was paused.
// Number of seconds displayed
// on the stopwatch.
private int seconds = 0;
// Is the stopwatch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null) {
// Get the previous state of the stopwatch
// if the activity has been
// destroyed and recreated.
seconds
= savedInstanceState
.getInt("seconds");
running
= savedInstanceState
.getBoolean("running");
wasRunning
= savedInstanceState
.getBoolean("wasRunning");
}
runTimer();
}
// Save the state of the stopwatch
// if it's about to be destroyed.
@Override
public void onSaveInstanceState(
Bundle savedInstanceState)
{
savedInstanceState
.putInt("seconds", seconds);
savedInstanceState
.putBoolean("running", running);
savedInstanceState
.putBoolean("wasRunning", wasRunning);
}
// If the activity is paused,
// stop the stopwatch.
@Override
protected void onPause()
{
super.onPause();
wasRunning = running;
running = false;
}
// If the activity is resumed,
// start the stopwatch
// again if it was running previously.
@Override
protected void onResume()
{
super.onResume();
if (wasRunning) {
running = true;
}
}
// Start the stopwatch running
// when the Start button is clicked.
// Below method gets called
// when the Start button is clicked.
public void onClickStart(View view)
{
running = true;
}
// Stop the stopwatch running
// when the Stop button is clicked.
// Below method gets called
// when the Stop button is clicked.
public void onClickStop(View view)
{
running = false;
}
// Reset the stopwatch when
// the Reset button is clicked.
// Below method gets called
// when the Reset button is clicked.
public void onClickReset(View view)
{
running = false;
seconds = 0;
}
// Sets the NUmber of seconds on the timer.
// The runTimer() method uses a Handler
// to increment the seconds and
// update the text view.
private void runTimer()
{
// Get the text view.
final TextView timeView
= (TextView)findViewById(
R.id.time_view);
// Creates a new Handler
final Handler handler
= new Handler();
// Call the post() method,
// passing in a new Runnable.
// The post() method processes
// code without a delay,
// so the code in the Runnable
// will run almost immediately.
handler.post(new Runnable() {
@Override
public void run()
{
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
// Format the seconds into hours, minutes,
// and seconds.
String time
= String
.format(Locale.getDefault(),
"%d:%02d:%02d", hours,
minutes, secs);
// Set the text view text.
timeView.setText(time);
// If running is true, increment the
// seconds variable.
if (running) {
seconds++;
}
// Post the code again
// with a delay of 1 second.
handler.postDelayed(this, 1000);
}
});
}
}