Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. package com.example.tomecki.alarmclock;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.os.CountDownTimer;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.NumberPicker;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. public class NewTimer extends Activity implements OnClickListener {
  19.  
  20. private Button buttonStartTime, buttonStopTime, one, two, three;
  21. private TextView textViewShowTime;
  22. private CountDownTimer countDownTimer;
  23. private long totalTimeCountInMilliseconds;
  24. private long timeBlinkInMilliseconds;
  25. private long timeBlinkInMillisecondstwo;
  26. private boolean blink;
  27. NumberPicker np1;
  28. NumberPicker np2;
  29. NumberPicker np3;
  30.  
  31. @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_new_timer);
  35.  
  36. buttonStartTime = (Button) findViewById(R.id.btnStartTime);
  37. buttonStopTime = (Button) findViewById(R.id.btnStopTime);
  38. textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
  39. //------------------------------
  40. one = (Button) findViewById(R.id.button_alarm2);
  41. two = (Button) findViewById(R.id.button_timer2);
  42. three = (Button) findViewById(R.id.button_stopwatch2);
  43.  
  44. buttonStartTime.setOnClickListener(this);
  45. buttonStopTime.setOnClickListener(this);
  46. //-----------------------
  47. one.setOnClickListener(this);
  48. two.setOnClickListener(this);
  49. three.setOnClickListener(this);
  50.  
  51. np1 = (NumberPicker) findViewById(R.id.numberTime3);
  52. np1.setMaxValue(59);
  53. np1.setMinValue(0);
  54. np1.setWrapSelectorWheel(true);
  55.  
  56. np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  57.  
  58. @Override
  59. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  60. }
  61. });
  62. np2 = (NumberPicker) findViewById(R.id.numberTime2);
  63. np2.setMaxValue(59);
  64. np2.setMinValue(0);
  65. np2.setWrapSelectorWheel(true);
  66.  
  67. np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  68.  
  69. @Override
  70. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  71. }
  72. });
  73. np3 = (NumberPicker) findViewById(R.id.numberTime1);
  74. np3.setMaxValue(100);
  75. np3.setMinValue(0);
  76. np3.setWrapSelectorWheel(true);
  77.  
  78. np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  79.  
  80. @Override
  81. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  82. }
  83. });
  84.  
  85.  
  86. }
  87.  
  88. //---------------------------------Menu + options---------------------------------------
  89. public void onClick(View v) {
  90.  
  91. switch (v.getId()) {
  92.  
  93. case R.id.button_alarm2:
  94. startActivity(new Intent(this, MainActivity.class));
  95. break;
  96.  
  97. case R.id.button_timer2:
  98.  
  99. startActivity(new Intent(this, NewTimer.class));
  100.  
  101. break;
  102.  
  103. case R.id.button_stopwatch2:
  104. startActivity(new Intent(this, NewStopwatch.class));
  105. break;
  106.  
  107. case R.id.btnStartTime:
  108. textViewShowTime.setTextAppearance(getApplicationContext(),
  109. R.style.normalText);
  110. setTimer();
  111. Toast.makeText(NewTimer.this, "Odliczanie zaczęte...",
  112. Toast.LENGTH_LONG).show();
  113. buttonStopTime.setVisibility(View.VISIBLE);
  114. buttonStartTime.setVisibility(View.GONE);
  115. startTimer();
  116. break;
  117. case R.id.btnStopTime:
  118. countDownTimer.cancel();
  119. buttonStartTime.setVisibility(View.VISIBLE);
  120. buttonStopTime.setVisibility(View.GONE);
  121. Toast.makeText(NewTimer.this, "Odliczanie przerwane",
  122. Toast.LENGTH_LONG).show();
  123. break;
  124.  
  125. default:
  126. break;
  127. }
  128.  
  129. }
  130. //---------------------------------End Menu + options-----------------------------
  131.  
  132. int time = 0;
  133.  
  134. private void setTimer() {
  135.  
  136.  
  137. time = (np1.getValue())+(np2.getValue() * 60)+(np3.getValue()*3600);
  138.  
  139. np1.setVisibility(View.GONE);
  140. np2.setVisibility(View.GONE);
  141. np3.setVisibility(View.GONE);
  142. textViewShowTime.setVisibility(View.VISIBLE);
  143.  
  144. totalTimeCountInMilliseconds = time * 1000;
  145.  
  146. timeBlinkInMilliseconds = 30 * 1000;
  147.  
  148. }
  149.  
  150. private void startTimer() {
  151. countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 1000) {
  152.  
  153.  
  154. @Override
  155. public void onTick(long leftTimeInMilliseconds) {
  156. long seconds = leftTimeInMilliseconds / 1000;
  157.  
  158. if (time <= timeBlinkInMilliseconds) {
  159. timeBlinkInMillisecondstwo = time / 2;
  160.  
  161. if (leftTimeInMilliseconds <= timeBlinkInMillisecondstwo) {
  162. textViewShowTime.setTextAppearance(getApplicationContext(),
  163. R.style.blinkText);
  164.  
  165.  
  166. if (blink) {
  167. textViewShowTime.setVisibility(View.VISIBLE);
  168.  
  169. } else {
  170. textViewShowTime.setVisibility(View.INVISIBLE);
  171. }
  172.  
  173. blink = !blink;
  174. }
  175. } else {
  176.  
  177.  
  178. if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
  179. textViewShowTime.setTextAppearance(getApplicationContext(),
  180. R.style.blinkText);
  181.  
  182.  
  183. if (blink) {
  184. textViewShowTime.setVisibility(View.VISIBLE);
  185.  
  186. } else {
  187. textViewShowTime.setVisibility(View.INVISIBLE);
  188. }
  189.  
  190. blink = !blink;
  191. }
  192. }
  193.  
  194.  
  195. textViewShowTime.setText(String.format("%02d", seconds /3600)
  196. + ":" + String.format("%02d", seconds / 60)
  197. + ":" + String.format("%02d", seconds % 60));
  198. textViewShowTime.setTextSize(10);
  199.  
  200. }
  201.  
  202. @Override
  203. public void onFinish() {
  204. textViewShowTime.setText("Czas się skończył!");
  205. textViewShowTime.setVisibility(View.VISIBLE);
  206. buttonStartTime.setVisibility(View.VISIBLE);
  207. buttonStopTime.setVisibility(View.GONE);
  208. }
  209.  
  210. }.start();
  211.  
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement