Advertisement
Guest User

Countdown

a guest
May 26th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. public class TimerActivity extends Activity implements OnClickListener {
  2.  
  3.     private CountDownTimer countDownTimer;
  4.     private boolean timerHasStarted = false;
  5.     private Button startB;
  6.     public TextView text;
  7.     public String time;
  8.     private long startTime = 30 * 1000;
  9.     private final long interval = 1 * 1000;
  10.     private EditText timeedit;
  11.  
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_countdown);
  16.         startB = (Button) this.findViewById(R.id.button);
  17.         startB.setOnClickListener(this);
  18.         text = (TextView) this.findViewById(R.id.timer);
  19.         timeedit = (EditText) findViewById(R.id.timeedit);
  20.         countDownTimer = new MyCountDownTimer(startTime, interval);
  21.         time = timeedit.getText().toString();
  22.         text.setText(time); //+ String.valueOf(startTime/1000)
  23.     }
  24.  
  25.     @Override
  26.     public void onClick(View v) {
  27.         if (!timerHasStarted) {
  28.             countDownTimer.start();
  29.             timerHasStarted = true;
  30.             startB.setText("STOP");
  31.         } else {
  32.             countDownTimer.cancel();
  33.             timerHasStarted = false;
  34.             startTime = 30 * 1000;
  35.             startB.setText("RESTART");
  36.         }
  37.     }
  38.  
  39.  
  40.     public class MyCountDownTimer extends CountDownTimer {
  41.         public MyCountDownTimer(long startTime, long interval) {
  42.             super(startTime, interval);
  43.         }
  44.  
  45.         @Override
  46.         public void onFinish() {
  47.             text.setText("Time's up!");
  48.         }
  49.  
  50.         @Override
  51.         public void onTick(long millisUntilFinished) {
  52.             text.setText("" + millisUntilFinished / 1000);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement