Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package com.example.oviktor.stopwatch;
  2.  
  3. import android.os.Looper;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10.  
  11.  
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. import java.util.logging.LogRecord;
  15.  
  16. //
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19.     private volatile int seconds = 0;
  20.     private volatile boolean running = false;
  21.     TextView textView;
  22.     Timer timer = null;
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         textView = (TextView)findViewById(R.id.time_view);
  28.  
  29.         Button start = (Button) findViewById(R.id.btnStart);
  30.         start.setOnClickListener(new View.OnClickListener() {
  31.             @Override
  32.             public void onClick(View v) {
  33.                 onClickStart();
  34.             }
  35.         });
  36.  
  37.         Button stop = (Button) findViewById(R.id.btnStop);
  38.         stop.setOnClickListener(new View.OnClickListener() {
  39.             @Override
  40.             public void onClick(View v) {
  41.                 onClickStop();
  42.             }
  43.         });
  44.  
  45.         Button reset = (Button) findViewById(R.id.btnReset);
  46.         reset.setOnClickListener(new View.OnClickListener() {
  47.             @Override
  48.             public void onClick(View v) {
  49.                 onClickReset();
  50.             }
  51.         });
  52.         runTimer();
  53.     }
  54.     @Override
  55.     public void onDestroy(){
  56.         if(timer!=null)
  57.             timer.cancel();
  58.         super.onDestroy();
  59.     }
  60.  
  61.     public void onClickStart() {
  62.         running = true;
  63.     }
  64.  
  65.     public void onClickStop() {
  66.         running = false;
  67.     }
  68.  
  69.     public void onClickReset() {
  70.         running = false;
  71.         seconds = 0;
  72.  
  73.     }
  74.    
  75.     private void runTimer(){
  76.  
  77.  
  78.         timer = new Timer();
  79.         timer.schedule(new TimerTask() {
  80.             @Override
  81.             public void run() {
  82.  
  83.                 if (running)
  84.                 seconds++;
  85.  
  86.                final int hours = seconds/3600;
  87.                final int minutes = (seconds%3600)/60;
  88.                final int sec = seconds%60;
  89.                //final double milisec = seconds/1000;
  90.  
  91.                 runOnUiThread(new Runnable() {
  92.                     @Override
  93.                     public void run() {
  94.                         textView.setText(String.format("%d:%02d:%02d", hours, minutes, sec));
  95.  
  96.                     }
  97.                 });
  98.  
  99.  
  100.             }
  101.         }, 0, 1000);
  102.         /*
  103.         textView.post(new Runnable() {
  104.             @Override
  105.             public void run() {
  106.                 int hours = seconds/3600;
  107.                 int minutes = (seconds%3600)/60;
  108.                 int sec = seconds%60;
  109.  
  110.                 String time = String.format("%d:%02d:%02d", hours, minutes, sec);
  111.                 textView.setText(time);
  112.                 if (running){
  113.                     seconds++;
  114.                 }
  115.                 textView.removeCallbacks(this);
  116.                 textView.postDelayed(this,1000);
  117.  
  118.             }
  119.         });
  120. */
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement