-
Notifications
You must be signed in to change notification settings - Fork 0
/
codingsaathi_taskno5
59 lines (51 loc) · 1.75 KB
/
codingsaathi_taskno5
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
package com.example.stopwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button startbutton,pausebutton,resetbutton;
private Chronometer chronometer;
private long pasoffset;
boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbutton=findViewById(R.id.startbutton);
pausebutton=findViewById(R.id.pausebutton);
resetbutton=findViewById(R.id.resetbutton);
chronometer=findViewById(R.id.chronometer);
startbutton.setOnClickListener(this);
pausebutton.setOnClickListener(this);
resetbutton.setOnClickListener(this);
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.startbutton:
if(!running)
{
chronometer.setBase(SystemClock.elapsedRealtime()-pasoffset);
chronometer.start();
running=true;
}
break;
case R.id.pausebutton:
if(running)
{
chronometer.stop();
pasoffset=SystemClock.elapsedRealtime()-chronometer.getBase();
running=false;
}
break;
case R.id.resetbutton:
chronometer.setBase(SystemClock.elapsedRealtime());
pasoffset=0;
break;
}
}
}