Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mloubakehotmail.cronometrocomcontadorregressivo;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.os.CountDownTimer;
- import android.view.View;
- public class TerceiraActivity extends AppCompatActivity
- {
- EditText editText;
- Button button;
- TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_principal);
- editText = (EditText) findViewById(R.id.editText);
- button = (Button) findViewById(R.id.button);
- textView = (TextView) findViewById(R.id.textView);
- button.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- String text = editText.getText().toString();
- if(!text.equalsIgnoreCase(""))
- {
- int seconds = Integer.valueOf(text);
- CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000)
- {
- @Override
- public void onTick(long millis)
- {
- textView.setText("seconds: " + (int) (millis / 1000));
- }
- @Override
- public void onFinish()
- {
- textView.setText("Done");
- }
- }.start();
- }
- }
- });
- }
- }
- /*
- //Get reference of the XML layout's widgets
- final TextView tView = (TextView) findViewById(R.id.txtContador);
- final Button btnStart = (Button) findViewById(R.id.btnIniciar);
- final Button btnPause = (Button) findViewById(R.id.btnPausar);
- final Button btnReset = (Button) findViewById(R.id.btnResetar);
- final Button btnCancel = (Button) findViewById(R.id.btnCancel);
- //Initially disabled the pause, resume and cancel button
- btnPause.setEnabled(false);
- btnReset.setEnabled(false);
- btnCancel.setEnabled(false);
- //Set a Click Listener for start button
- btnStart.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v){
- isPaused = true;
- isCanceled = true;
- //Disable the start and pause button
- btnStart.setEnabled(false);
- btnReset.setEnabled(false);
- //Enabled the pause and cancel button
- btnPause.setEnabled(true);
- btnCancel.setEnabled(true);
- long millisInFuture = 30000; //30 seconds
- long countDownInterval = 1000; //1 second
- //Initialize a new CountDownTimer instance
- timer = new CountDownTimer(millisInFuture,countDownInterval){
- public void onTick(long millisUntilFinished){
- //do something in every tick
- if(isPaused || isCanceled)
- {
- //If the user request to cancel or paused the
- //CountDownTimer we will cancel the current instance
- cancel();
- }
- else {
- //Display the remaining seconds to app interface
- //1 second = 1000 milliseconds
- tView.setText("" + millisUntilFinished / 1000);
- //Put count down timer remaining time in a variable
- timeRemaining = millisUntilFinished;
- }
- }
- public void onFinish(){
- //Do something when count down finished
- tView.setText("Done");
- //Enable the start button
- btnStart.setEnabled(true);
- //Disable the pause, resume and cancel button
- btnPause.setEnabled(false);
- btnReset.setEnabled(false);
- btnCancel.setEnabled(false);
- }
- }.start();
- }
- });
- //Set a Click Listener for pause button
- btnPause.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v){
- //When user request to pause the CountDownTimer
- isPaused = true;
- //Enable the resume and cancel button
- btnReset.setEnabled(true);
- btnCancel.setEnabled(true);
- //Disable the start and pause button
- btnStart.setEnabled(false);
- btnPause.setEnabled(false);
- }
- });
- //Set a Click Listener for resume button
- btnReset.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v)
- {
- //Disable the start and resume button
- btnStart.setEnabled(false);
- btnReset.setEnabled(false);
- //Enable the pause and cancel button
- btnPause.setEnabled(true);
- btnCancel.setEnabled(true);
- //Specify the current state is not paused and canceled.
- isPaused = false;
- isCanceled = false;
- //Initialize a new CountDownTimer instance
- long millisInFuture = timeRemaining;
- long countDownInterval = 1000;
- new CountDownTimer(millisInFuture, countDownInterval){
- public void onTick(long millisUntilFinished){
- //Do something in every tick
- if(isPaused || isCanceled)
- {
- //If user requested to pause or cancel the count down timer
- cancel();
- }
- else {
- tView.setText("" + millisUntilFinished / 1000);
- //Put count down timer remaining time in a variable
- timeRemaining = millisUntilFinished;
- }
- }
- public void onFinish(){
- //Do something when count down finished
- tView.setText("Done");
- //Disable the pause, resume and cancel button
- btnPause.setEnabled(false);
- btnReset.setEnabled(false);
- btnCancel.setEnabled(false);
- //Enable the start button
- btnStart.setEnabled(true);
- }
- }.start();
- //Set a Click Listener for cancel/stop button
- btnCancel.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v){
- //When user request to cancel the CountDownTimer
- isCanceled = true;
- //Disable the cancel, pause and resume button
- btnPause.setEnabled(false);
- btnReset.setEnabled(false);
- btnCancel.setEnabled(false);
- //Enable the start button
- btnStart.setEnabled(true);
- //Notify the user that CountDownTimer is canceled/stopped
- tView.setText("CountDownTimer Canceled/stopped.");
- }
- });
- }
- });
- //Set a Click Listener for cancel/stop button
- btnCancel.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v){
- //When user request to cancel the CountDownTimer
- isCanceled = true;
- //Disable the cancel, pause and resume button
- btnPause.setEnabled(false);
- btnReset.setEnabled(false);
- btnCancel.setEnabled(false);
- //Enable the start button
- btnStart.setEnabled(true);
- //Notify the user that CountDownTimer is canceled/stopped
- tView.setText("CountDownTimer Canceled/stopped.");
- }
- });
- }
- }
- /*@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement