-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.java
More file actions
64 lines (57 loc) · 2.02 KB
/
Copy pathcounter.java
File metadata and controls
64 lines (57 loc) · 2.02 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
package com.example.counterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button start;
Button stop;
Button reset;
TextView counter;
static int count = 1;
Handler clickHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
stop = findViewById(R.id.stop);
counter = findViewById(R.id.count);
reset = findViewById(R.id.reset);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "counter started", Toast.LENGTH_SHORT).show();
clickHandler.postDelayed(updateTimerThread,0);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickHandler.removeCallbacks(updateTimerThread);
Toast.makeText(MainActivity.this, "counter stopped, count = "+count, Toast.LENGTH_SHORT).show();
// count = 0;
counter.setText(""+count);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "counter reset", Toast.LENGTH_SHORT).show();
count = 0;
counter.setText(""+count);
}
});
}
private final Runnable updateTimerThread = new Runnable() {
@Override
public void run() {
counter.setText(""+count);
clickHandler.postDelayed(this,1000);
count++;
}
};
}