Advertisement
Koryagin_Sergey

Timer

Jan 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.31 KB | None | 0 0
  1. package sourceit.com.timer;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.widget.Button;
  6.  
  7. import butterknife.BindView;
  8. import butterknife.ButterKnife;
  9. import butterknife.OnClick;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12. //
  13.     @BindView(R.id.action_timer)
  14.     Button actionTimer;
  15.     @BindView(R.id.action_stopwatch)
  16.     Button actionStopwatch;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.         ButterKnife.bind(this);
  23.  
  24.         getSupportFragmentManager()
  25.                 .beginTransaction()
  26.                 .replace(R.id.container, TimerFragment.newInstance())
  27.                 .commit();
  28.     }
  29.  
  30.     @OnClick(R.id.action_stopwatch)
  31.     public void onClickStopwatch() {
  32.         getSupportFragmentManager()
  33.                 .beginTransaction()
  34.                 .replace(R.id.container,
  35.                         StopwatchFragment.newInstance())
  36.                 .addToBackStack(null)
  37.                 .commit();
  38.     }
  39.  
  40.     @OnClick(R.id.action_timer)
  41.     public void onClickTimer() {
  42.         getSupportFragmentManager()
  43.                 .beginTransaction()
  44.                 .replace(R.id.container,
  45.                         TimerFragment.newInstance())
  46.                 .addToBackStack(null)
  47.                 .commit();
  48.     }
  49.  
  50. }
  51. ________________________________________________________________________________________________
  52. package sourceit.com.timer;
  53.  
  54. import android.os.Bundle;
  55. import android.os.Handler;
  56. import android.os.Message;
  57. import android.support.v4.app.Fragment;
  58. import android.view.LayoutInflater;
  59. import android.view.View;
  60. import android.view.ViewGroup;
  61. import android.widget.TextView;
  62.  
  63. import butterknife.BindView;
  64. import butterknife.ButterKnife;
  65. import butterknife.OnClick;
  66. import butterknife.OnLongClick;
  67. import butterknife.Unbinder;
  68.  
  69. public class TimerFragment extends Fragment {
  70.  
  71.     private static final int START = 1;
  72.     private static final int STOP = 2;
  73.     int time = 30;
  74.     int timeLongClick = 30;
  75.     private boolean isStarted;
  76.     @BindView(R.id.text_timer)
  77.     TextView textTimer;
  78.     Handler h = new Handler() {
  79.         @Override
  80.         public void dispatchMessage(Message msg) {
  81.             if (msg.what == START) {
  82.                 textTimer.setText(String.valueOf(time));
  83.                 time--;
  84.                 if (time >= 0) {
  85.                     h.sendEmptyMessageDelayed(START, 1_000);
  86.                 }
  87.             } else if (msg.what == STOP) {
  88.                 stopTimer();
  89.             }
  90.         }
  91.     };
  92.     Unbinder unbinder;
  93.  
  94.     public static TimerFragment newInstance() {
  95.         TimerFragment fragment = new TimerFragment();
  96.         return fragment;
  97.     }
  98.  
  99.     @Override
  100.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  101.                              Bundle savedInstanceState) {
  102.         View root = inflater.inflate(R.layout.fragment_timer, container, false);
  103.         unbinder = ButterKnife.bind(this, root);
  104.         startTimer();
  105.         return root;
  106.     }
  107.  
  108.     public void startTimer() {
  109.         isStarted = true;
  110.         h.sendEmptyMessage(START);
  111.     }
  112.  
  113.     public void stopTimer() {
  114.         isStarted = false;
  115.         h.removeMessages(START);
  116.     }
  117.  
  118.     @OnClick(R.id.text_timer)
  119.     public void onNumberClick() {
  120.         if (isStarted) {
  121.             stopTimer();
  122.         } else {
  123.             startTimer();
  124.         }
  125.     }
  126.  
  127.     @OnLongClick(R.id.text_timer)
  128.     public boolean onNumberLongClick() {
  129.         stopTimer();
  130.         textTimer.setText(String.valueOf(timeLongClick));
  131.         time = timeLongClick;
  132.         return true;
  133.     }
  134.  
  135.     @Override
  136.     public void onDestroyView() {
  137.         unbinder.unbind();
  138.         h.removeMessages(START);
  139.         h.removeMessages(STOP);
  140.         super.onDestroyView();
  141.     }
  142. }
  143. _____________________________________________________________________________________
  144.  
  145. package sourceit.com.timer;
  146.  
  147. import android.os.Bundle;
  148. import android.os.Handler;
  149. import android.os.Message;
  150. import android.support.v4.app.Fragment;
  151. import android.view.LayoutInflater;
  152. import android.view.View;
  153. import android.view.ViewGroup;
  154. import android.widget.TextView;
  155.  
  156. import butterknife.BindView;
  157. import butterknife.ButterKnife;
  158. import butterknife.OnClick;
  159. import butterknife.OnLongClick;
  160. import butterknife.Unbinder;
  161.  
  162. public class StopwatchFragment extends Fragment {
  163.  
  164.     private static final int START = 1;
  165.     private static final int STOP = 2;
  166.     private int seconds = 0;
  167.     private boolean isStarted;
  168.     @BindView(R.id.text_stopwatch)
  169.     TextView textStopwatch;
  170.     Handler h = new Handler() {
  171.         @Override
  172.         public void dispatchMessage(Message msg) {
  173.             int hours = seconds / 3600;
  174.             int minutes = (seconds % 3600) / 60;
  175.             int sec = seconds % 60;
  176.             String time = String.format("%d:%02d:%02d", hours, minutes, sec);
  177.             textStopwatch.setText(time);
  178.             if (msg.what == START) {
  179.                 seconds++;
  180.                 if (isStarted) {
  181.                     h.sendEmptyMessageDelayed(START, 1000);
  182.                 }
  183.             } else if (msg.what == STOP) {
  184.                 stopStopwatch();
  185.             }
  186.         }
  187.     };
  188.     Unbinder unbinder;
  189.  
  190.     public static StopwatchFragment newInstance() {
  191.         StopwatchFragment fragment = new StopwatchFragment();
  192.         return fragment;
  193.     }
  194.  
  195.     @Override
  196.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  197.                              Bundle savedInstanceState) {
  198.         View root = inflater.inflate(R.layout.fragment_stopwatch, container, false);
  199.         unbinder = ButterKnife.bind(this, root);
  200.         startStopwatch();
  201.         return root;
  202.     }
  203.  
  204.     public void startStopwatch() {
  205.         isStarted = true;
  206.         h.sendEmptyMessage(START);
  207.     }
  208.  
  209.     public void stopStopwatch() {
  210.         isStarted = false;
  211.         h.removeMessages(START);
  212.     }
  213.  
  214.     @OnClick(R.id.text_stopwatch)
  215.     public void onStopwatchClick() {
  216.         if (isStarted) {
  217.             stopStopwatch();
  218.         } else {
  219.             startStopwatch();
  220.         }
  221.     }
  222.  
  223.     @OnLongClick(R.id.text_stopwatch)
  224.     public boolean onStopWatchLongClick() {
  225.         stopStopwatch();
  226.         seconds = 0;
  227.         return true;
  228.     }
  229.  
  230.     @Override
  231.     public void onDestroyView() {
  232.         unbinder.unbind();
  233.         h.removeMessages(START);
  234.         h.removeMessages(STOP);
  235.         super.onDestroyView();
  236.     }
  237.  
  238. }
  239.  
  240. ______________________________________________________________
  241.  
  242. <?xml version="1.0" encoding="utf-8"?>
  243. <LinearLayout
  244.     xmlns:android="http://schemas.android.com/apk/res/android"
  245.     xmlns:tools="http://schemas.android.com/tools"
  246.     android:layout_width="match_parent"
  247.     android:layout_height="match_parent"
  248.     android:orientation="vertical"
  249.     tools:context="sourceit.com.timer.MainActivity">
  250.  
  251.     <LinearLayout
  252.         android:layout_width="match_parent"
  253.         android:layout_height="wrap_content"
  254.         android:orientation="horizontal">
  255.  
  256.  
  257.         <Button
  258.             android:id="@+id/action_timer"
  259.             android:layout_width="0dp"
  260.             android:layout_height="wrap_content"
  261.             android:layout_weight="1"
  262.             android:text="@string/action_timer" />
  263.  
  264.         <Button
  265.             android:id="@+id/action_stopwatch"
  266.             android:layout_width="0dp"
  267.             android:layout_height="wrap_content"
  268.             android:layout_weight="1"
  269.             android:text="@string/action_stopwatch" />
  270.  
  271.     </LinearLayout>
  272.  
  273.     <FrameLayout
  274.         android:id="@+id/container"
  275.         android:layout_width="match_parent"
  276.         android:layout_height="match_parent">
  277.  
  278.     </FrameLayout>
  279.  
  280.  
  281. </LinearLayout>
  282.  
  283. ______________________________________________________________________
  284.  
  285. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  286.     android:id="@+id/activity_second"
  287.     android:layout_width="match_parent"
  288.     android:layout_height="match_parent"
  289.     android:paddingBottom="@dimen/activity_vertical_margin"
  290.     android:paddingLeft="@dimen/activity_horizontal_margin"
  291.     android:paddingRight="@dimen/activity_horizontal_margin"
  292.     android:paddingTop="@dimen/activity_vertical_margin">
  293.  
  294.     <TextView
  295.         android:id="@+id/text_timer"
  296.         android:layout_width="wrap_content"
  297.         android:layout_height="wrap_content"
  298.         android:layout_centerInParent="true"
  299.         android:textSize="80sp" />
  300. </RelativeLayout>
  301.  
  302. _________________________________________________________________
  303.  
  304. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  305.     android:id="@+id/activity_second"
  306.     android:layout_width="match_parent"
  307.     android:layout_height="match_parent"
  308.     android:paddingBottom="@dimen/activity_vertical_margin"
  309.     android:paddingLeft="@dimen/activity_horizontal_margin"
  310.     android:paddingRight="@dimen/activity_horizontal_margin"
  311.     android:paddingTop="@dimen/activity_vertical_margin">
  312.  
  313.     <TextView
  314.         android:id="@+id/text_stopwatch"
  315.         android:layout_width="wrap_content"
  316.         android:layout_height="wrap_content"
  317.         android:layout_centerInParent="true"
  318.         android:textSize="50sp" />
  319. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement