-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopw.java
More file actions
82 lines (56 loc) · 1.77 KB
/
Copy pathstopw.java
File metadata and controls
82 lines (56 loc) · 1.77 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
package com.example.stopwatch2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class stopw extends AppCompatActivity {
private int seconds=0;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopw);
runTimer();
}
public void onClickStart(View view)
{
running=true;
Toast.makeText(getApplicationContext(),"stopwatch start",Toast.LENGTH_SHORT).show();
}
public void onClickStop(View view)
{
running=false;
Toast.makeText(getApplicationContext(),"stopwatch stopped",Toast.LENGTH_SHORT).show();
}
public void onClickReset(View view)
{
running=false;
seconds=0;
Toast.makeText(getApplicationContext(),"stopwatch reset",Toast.LENGTH_SHORT).show();
}
private void runTimer()
{
final TextView time_view=findViewById(R.id.time_view);
final Handler handler=new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours=seconds/3600;
int minutes=(seconds%3600)/60;
int secs=seconds%60;
String time= String.format("%d:%02d:%02d",hours,minutes,secs);
time_view.setText(time);
if(running)
{
seconds++;
}
handler.postDelayed(this,1000);
}
});
}
}