-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
99 lines (83 loc) · 2.78 KB
/
MainActivity.java
File metadata and controls
99 lines (83 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package edu.vt.amm28053.androidquickstart;
import android.app.Activity;
import android.os.SystemClock;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
/*
* These are our control buttons.
*/
private Button start, stop, resume, reset;
private ChronometerMilli timer;
private long elapsedTime = 0L;
/**
* This is the first callback method called in the Activity lifecycle.
* Generally, this is where initialization of variables happens.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the layout of this activity to be the layout defined in activity_main.xml
setContentView(R.layout.activity_main);
// findViewById finds a View contained within the Activity's layout with the id passed in
// View ids are defined in the layout file.
start = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
resume = (Button)findViewById(R.id.resume);
reset = (Button)findViewById(R.id.reset);
timer = (ChronometerMilli)findViewById(R.id.chronometer);
resetTimer();
start.setOnClickListener(this);
stop.setOnClickListener(this);
resume.setOnClickListener(this);
reset.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
startTimer();
hideButtons(start);
showButtons(stop);
break;
case R.id.stop:
pauseTimer();
hideButtons(stop);
showButtons(resume, reset);
break;
case R.id.resume:
startTimer();
hideButtons(resume, reset);
showButtons(stop);
break;
case R.id.reset:
resetTimer();
hideButtons(resume, reset);
showButtons(start);
break;
}
}
private void showButtons(Button... buttons) {
for (Button b : buttons) {
b.setVisibility(View.VISIBLE);
}
}
private void hideButtons(Button... buttons) {
for (Button b : buttons) {
b.setVisibility(View.GONE);
}
}
private void startTimer() {
timer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
timer.start();
}
private void pauseTimer() {
elapsedTime = (SystemClock.elapsedRealtime() - timer.getBase());
timer.stop();
}
private void resetTimer() {
elapsedTime = 0;
timer.setText("00:00.00");
}
}