Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tomecki.alarmclock;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AlphaAnimation;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.NumberPicker;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class NewTimer extends Activity implements OnClickListener {
- private Button buttonStartTime, buttonStopTime, one, two, three;
- private TextView textViewShowTime;
- private CountDownTimer countDownTimer;
- private long totalTimeCountInMilliseconds;
- private long timeBlinkInMilliseconds;
- private long timeBlinkInMillisecondstwo;
- private boolean blink;
- NumberPicker np1;
- NumberPicker np2;
- NumberPicker np3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_new_timer);
- buttonStartTime = (Button) findViewById(R.id.btnStartTime);
- buttonStopTime = (Button) findViewById(R.id.btnStopTime);
- textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
- //------------------------------
- one = (Button) findViewById(R.id.button_alarm2);
- two = (Button) findViewById(R.id.button_timer2);
- three = (Button) findViewById(R.id.button_stopwatch2);
- buttonStartTime.setOnClickListener(this);
- buttonStopTime.setOnClickListener(this);
- //-----------------------
- one.setOnClickListener(this);
- two.setOnClickListener(this);
- three.setOnClickListener(this);
- np1 = (NumberPicker) findViewById(R.id.numberTime3);
- np1.setMaxValue(59);
- np1.setMinValue(0);
- np1.setWrapSelectorWheel(true);
- np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
- @Override
- public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
- }
- });
- np2 = (NumberPicker) findViewById(R.id.numberTime2);
- np2.setMaxValue(59);
- np2.setMinValue(0);
- np2.setWrapSelectorWheel(true);
- np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
- @Override
- public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
- }
- });
- np3 = (NumberPicker) findViewById(R.id.numberTime1);
- np3.setMaxValue(100);
- np3.setMinValue(0);
- np3.setWrapSelectorWheel(true);
- np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
- @Override
- public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
- }
- });
- }
- //---------------------------------Menu + options---------------------------------------
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button_alarm2:
- startActivity(new Intent(this, MainActivity.class));
- break;
- case R.id.button_timer2:
- startActivity(new Intent(this, NewTimer.class));
- break;
- case R.id.button_stopwatch2:
- startActivity(new Intent(this, NewStopwatch.class));
- break;
- case R.id.btnStartTime:
- textViewShowTime.setTextAppearance(getApplicationContext(),
- R.style.normalText);
- setTimer();
- Toast.makeText(NewTimer.this, "Odliczanie zaczęte...",
- Toast.LENGTH_LONG).show();
- buttonStopTime.setVisibility(View.VISIBLE);
- buttonStartTime.setVisibility(View.GONE);
- startTimer();
- break;
- case R.id.btnStopTime:
- countDownTimer.cancel();
- buttonStartTime.setVisibility(View.VISIBLE);
- buttonStopTime.setVisibility(View.GONE);
- Toast.makeText(NewTimer.this, "Odliczanie przerwane",
- Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- }
- //---------------------------------End Menu + options-----------------------------
- int time = 0;
- private void setTimer() {
- time = (np1.getValue())+(np2.getValue() * 60)+(np3.getValue()*3600);
- np1.setVisibility(View.GONE);
- np2.setVisibility(View.GONE);
- np3.setVisibility(View.GONE);
- textViewShowTime.setVisibility(View.VISIBLE);
- totalTimeCountInMilliseconds = time * 1000;
- timeBlinkInMilliseconds = 30 * 1000;
- }
- private void startTimer() {
- countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
- @Override
- public void onTick(long leftTimeInMilliseconds) {
- if (time <= timeBlinkInMilliseconds) {
- timeBlinkInMillisecondstwo = time / 2;
- if (leftTimeInMilliseconds <= timeBlinkInMillisecondstwo) {
- textViewShowTime.setTextAppearance(getApplicationContext(),
- R.style.blinkText);
- AlphaAnimation animation1 = new AlphaAnimation(0.02f, 0.01f);
- animation1.setDuration(1000);
- animation1.setStartOffset(5000);
- animation1.setFillAfter(true);
- textViewShowTime.startAnimation(animation1);
- }
- } else {
- if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
- textViewShowTime.setTextAppearance(getApplicationContext(),
- R.style.blinkText);
- AlphaAnimation animation1 = new AlphaAnimation(0.02f, 0.01f);
- animation1.setDuration(1000);
- animation1.setStartOffset(5000);
- animation1.setFillAfter(true);
- textViewShowTime.startAnimation(animation1);
- }
- }
- Date date = new Date(leftTimeInMilliseconds);
- DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); //tutaj możesz podać różne warianty
- String dateFormatted = formatter.format(date);
- textViewShowTime.setText(String.format(dateFormatted));
- textViewShowTime.setTextSize(10);
- }
- @Override
- public void onFinish() {
- textViewShowTime.setText("Czas się skończył!");
- textViewShowTime.setVisibility(View.VISIBLE);
- buttonStartTime.setVisibility(View.VISIBLE);
- buttonStopTime.setVisibility(View.GONE);
- }
- }.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement