Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.50 KB | None | 0 0
  1. package com.mloubakehotmail.cronometrocomcontadorregressivo;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.os.CountDownTimer;
  9. import android.view.View;
  10.  
  11. public class TerceiraActivity extends AppCompatActivity
  12. {
  13.     EditText editText;
  14.     Button button;
  15.     TextView textView;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState)
  19.     {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_principal);
  22.  
  23.         editText = (EditText) findViewById(R.id.editText);
  24.         button = (Button) findViewById(R.id.button);
  25.         textView = (TextView) findViewById(R.id.textView);
  26.  
  27.  
  28.         button.setOnClickListener(new View.OnClickListener()
  29.         {
  30.             @Override
  31.             public void onClick(View view)
  32.             {
  33.                 String text = editText.getText().toString();
  34.                 if(!text.equalsIgnoreCase(""))
  35.                 {
  36.                     int seconds = Integer.valueOf(text);
  37.                     CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000)
  38.                     {
  39.                         @Override
  40.                         public void onTick(long millis)
  41.                         {
  42.                             textView.setText("seconds: " + (int) (millis / 1000));
  43.                         }
  44.  
  45.                         @Override
  46.                         public void onFinish()
  47.                         {
  48.                             textView.setText("Done");
  49.                         }
  50.                     }.start();
  51.  
  52.                 }
  53.             }
  54.         });
  55.  
  56.     }
  57. }  
  58.        
  59.         /*
  60.         //Get reference of the XML layout's widgets
  61.         final TextView tView = (TextView) findViewById(R.id.txtContador);
  62.         final Button btnStart = (Button) findViewById(R.id.btnIniciar);
  63.         final Button btnPause = (Button) findViewById(R.id.btnPausar);
  64.         final Button btnReset = (Button) findViewById(R.id.btnResetar);
  65.         final Button btnCancel = (Button) findViewById(R.id.btnCancel);
  66.  
  67.         //Initially disabled the pause, resume and cancel button
  68.         btnPause.setEnabled(false);
  69.         btnReset.setEnabled(false);
  70.         btnCancel.setEnabled(false);
  71.  
  72.        
  73.        
  74.        
  75. //Set a Click Listener for start button
  76.         btnStart.setOnClickListener(new OnClickListener(){
  77.             @Override
  78.             public void onClick(View v){
  79.  
  80.                 isPaused = true;
  81.                 isCanceled = true;
  82.  
  83.                 //Disable the start and pause button
  84.                 btnStart.setEnabled(false);
  85.                 btnReset.setEnabled(false);
  86.                 //Enabled the pause and cancel button
  87.                 btnPause.setEnabled(true);
  88.                 btnCancel.setEnabled(true);
  89.  
  90.  
  91.                 long millisInFuture = 30000; //30 seconds
  92.                 long countDownInterval = 1000; //1 second
  93.  
  94.  
  95.                 //Initialize a new CountDownTimer instance
  96.                 timer = new CountDownTimer(millisInFuture,countDownInterval){
  97.                     public void onTick(long millisUntilFinished){
  98.                         //do something in every tick
  99.                         if(isPaused || isCanceled)
  100.                         {
  101.                             //If the user request to cancel or paused the
  102.                             //CountDownTimer we will cancel the current instance
  103.                             cancel();
  104.                         }
  105.                         else {
  106.                             //Display the remaining seconds to app interface
  107.                             //1 second = 1000 milliseconds
  108.                             tView.setText("" + millisUntilFinished / 1000);
  109.                             //Put count down timer remaining time in a variable
  110.                             timeRemaining = millisUntilFinished;
  111.                         }
  112.                     }
  113.                     public void onFinish(){
  114.                         //Do something when count down finished
  115.                         tView.setText("Done");
  116.  
  117.                         //Enable the start button
  118.                         btnStart.setEnabled(true);
  119.                         //Disable the pause, resume and cancel button
  120.                         btnPause.setEnabled(false);
  121.                         btnReset.setEnabled(false);
  122.                         btnCancel.setEnabled(false);
  123.                     }
  124.                 }.start();
  125.             }
  126.         });
  127.  
  128.         //Set a Click Listener for pause button
  129.         btnPause.setOnClickListener(new OnClickListener(){
  130.             @Override
  131.             public void onClick(View v){
  132.                 //When user request to pause the CountDownTimer
  133.                 isPaused = true;
  134.  
  135.                 //Enable the resume and cancel button
  136.                 btnReset.setEnabled(true);
  137.                 btnCancel.setEnabled(true);
  138.                 //Disable the start and pause button
  139.                 btnStart.setEnabled(false);
  140.                 btnPause.setEnabled(false);
  141.             }
  142.         });
  143.  
  144.         //Set a Click Listener for resume button
  145.         btnReset.setOnClickListener(new OnClickListener(){
  146.             @Override
  147.             public void onClick(View v)
  148.             {
  149.                 //Disable the start and resume button
  150.                 btnStart.setEnabled(false);
  151.                 btnReset.setEnabled(false);
  152.                 //Enable the pause and cancel button
  153.                 btnPause.setEnabled(true);
  154.                 btnCancel.setEnabled(true);
  155.  
  156.                 //Specify the current state is not paused and canceled.
  157.                 isPaused = false;
  158.                 isCanceled = false;
  159.  
  160.                 //Initialize a new CountDownTimer instance
  161.                 long millisInFuture = timeRemaining;
  162.                 long countDownInterval = 1000;
  163.                 new CountDownTimer(millisInFuture, countDownInterval){
  164.                     public void onTick(long millisUntilFinished){
  165.                         //Do something in every tick
  166.                         if(isPaused || isCanceled)
  167.                         {
  168.                             //If user requested to pause or cancel the count down timer
  169.                             cancel();
  170.                         }
  171.                         else {
  172.                             tView.setText("" + millisUntilFinished / 1000);
  173.                             //Put count down timer remaining time in a variable
  174.                             timeRemaining = millisUntilFinished;
  175.                         }
  176.                     }
  177.                     public void onFinish(){
  178.                         //Do something when count down finished
  179.                         tView.setText("Done");
  180.                         //Disable the pause, resume and cancel button
  181.                         btnPause.setEnabled(false);
  182.                         btnReset.setEnabled(false);
  183.                         btnCancel.setEnabled(false);
  184.                         //Enable the start button
  185.                         btnStart.setEnabled(true);
  186.                     }
  187.                 }.start();
  188.  
  189.                 //Set a Click Listener for cancel/stop button
  190.                 btnCancel.setOnClickListener(new OnClickListener(){
  191.                     @Override
  192.                     public void onClick(View v){
  193.                         //When user request to cancel the CountDownTimer
  194.                         isCanceled = true;
  195.  
  196.                         //Disable the cancel, pause and resume button
  197.                         btnPause.setEnabled(false);
  198.                         btnReset.setEnabled(false);
  199.                         btnCancel.setEnabled(false);
  200.                         //Enable the start button
  201.                         btnStart.setEnabled(true);
  202.  
  203.                         //Notify the user that CountDownTimer is canceled/stopped
  204.                         tView.setText("CountDownTimer Canceled/stopped.");
  205.                     }
  206.                 });
  207.             }
  208.         });
  209.  
  210.         //Set a Click Listener for cancel/stop button
  211.         btnCancel.setOnClickListener(new OnClickListener(){
  212.             @Override
  213.             public void onClick(View v){
  214.                 //When user request to cancel the CountDownTimer
  215.                 isCanceled = true;
  216.  
  217.                 //Disable the cancel, pause and resume button
  218.                 btnPause.setEnabled(false);
  219.                 btnReset.setEnabled(false);
  220.                 btnCancel.setEnabled(false);
  221.                 //Enable the start button
  222.                 btnStart.setEnabled(true);
  223.  
  224.                 //Notify the user that CountDownTimer is canceled/stopped
  225.                 tView.setText("CountDownTimer Canceled/stopped.");
  226.             }
  227.         });
  228.  
  229.     }
  230.  
  231. }
  232.     /*@Override
  233.     public boolean onCreateOptionsMenu(Menu menu) {
  234.         // Inflate the menu; this adds items to the action bar if it is present.
  235.         getMenuInflater().inflate(R.menu.menu_main, menu);
  236.         return true;
  237.     }
  238.  
  239.  
  240.     @Override
  241.     public boolean onOptionsItemSelected(MenuItem item) {
  242.         // Handle action bar item clicks here. The action bar will
  243.         // automatically handle clicks on the Home/Up button, so long
  244.         // as you specify a parent activity in AndroidManifest.xml.
  245.         int id = item.getItemId();
  246.  
  247.         //noinspection SimplifiableIfStatement
  248.         if (id == R.id.action_settings) {
  249.             return true;
  250.         }
  251.  
  252.         return super.onOptionsItemSelected(item);
  253.     }
  254. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement