Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TimerActivity extends Activity implements OnClickListener {
- private CountDownTimer countDownTimer;
- private boolean timerHasStarted = false;
- private Button startB;
- public TextView text;
- public String time;
- private long startTime = 30 * 1000;
- private final long interval = 1 * 1000;
- private EditText timeedit;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_countdown);
- startB = (Button) this.findViewById(R.id.button);
- startB.setOnClickListener(this);
- text = (TextView) this.findViewById(R.id.timer);
- timeedit = (EditText) findViewById(R.id.timeedit);
- countDownTimer = new MyCountDownTimer(startTime, interval);
- time = timeedit.getText().toString();
- text.setText(time); //+ String.valueOf(startTime/1000)
- }
- @Override
- public void onClick(View v) {
- if (!timerHasStarted) {
- countDownTimer.start();
- timerHasStarted = true;
- startB.setText("STOP");
- } else {
- countDownTimer.cancel();
- timerHasStarted = false;
- startTime = 30 * 1000;
- startB.setText("RESTART");
- }
- }
- public class MyCountDownTimer extends CountDownTimer {
- public MyCountDownTimer(long startTime, long interval) {
- super(startTime, interval);
- }
- @Override
- public void onFinish() {
- text.setText("Time's up!");
- }
- @Override
- public void onTick(long millisUntilFinished) {
- text.setText("" + millisUntilFinished / 1000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement