Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sourceit.com.timer;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Button;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- public class MainActivity extends AppCompatActivity {
- //
- @BindView(R.id.action_timer)
- Button actionTimer;
- @BindView(R.id.action_stopwatch)
- Button actionStopwatch;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- getSupportFragmentManager()
- .beginTransaction()
- .replace(R.id.container, TimerFragment.newInstance())
- .commit();
- }
- @OnClick(R.id.action_stopwatch)
- public void onClickStopwatch() {
- getSupportFragmentManager()
- .beginTransaction()
- .replace(R.id.container,
- StopwatchFragment.newInstance())
- .addToBackStack(null)
- .commit();
- }
- @OnClick(R.id.action_timer)
- public void onClickTimer() {
- getSupportFragmentManager()
- .beginTransaction()
- .replace(R.id.container,
- TimerFragment.newInstance())
- .addToBackStack(null)
- .commit();
- }
- }
- ________________________________________________________________________________________________
- package sourceit.com.timer;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import butterknife.OnLongClick;
- import butterknife.Unbinder;
- public class TimerFragment extends Fragment {
- private static final int START = 1;
- private static final int STOP = 2;
- int time = 30;
- int timeLongClick = 30;
- private boolean isStarted;
- @BindView(R.id.text_timer)
- TextView textTimer;
- Handler h = new Handler() {
- @Override
- public void dispatchMessage(Message msg) {
- if (msg.what == START) {
- textTimer.setText(String.valueOf(time));
- time--;
- if (time >= 0) {
- h.sendEmptyMessageDelayed(START, 1_000);
- }
- } else if (msg.what == STOP) {
- stopTimer();
- }
- }
- };
- Unbinder unbinder;
- public static TimerFragment newInstance() {
- TimerFragment fragment = new TimerFragment();
- return fragment;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_timer, container, false);
- unbinder = ButterKnife.bind(this, root);
- startTimer();
- return root;
- }
- public void startTimer() {
- isStarted = true;
- h.sendEmptyMessage(START);
- }
- public void stopTimer() {
- isStarted = false;
- h.removeMessages(START);
- }
- @OnClick(R.id.text_timer)
- public void onNumberClick() {
- if (isStarted) {
- stopTimer();
- } else {
- startTimer();
- }
- }
- @OnLongClick(R.id.text_timer)
- public boolean onNumberLongClick() {
- stopTimer();
- textTimer.setText(String.valueOf(timeLongClick));
- time = timeLongClick;
- return true;
- }
- @Override
- public void onDestroyView() {
- unbinder.unbind();
- h.removeMessages(START);
- h.removeMessages(STOP);
- super.onDestroyView();
- }
- }
- _____________________________________________________________________________________
- package sourceit.com.timer;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import butterknife.OnLongClick;
- import butterknife.Unbinder;
- public class StopwatchFragment extends Fragment {
- private static final int START = 1;
- private static final int STOP = 2;
- private int seconds = 0;
- private boolean isStarted;
- @BindView(R.id.text_stopwatch)
- TextView textStopwatch;
- Handler h = new Handler() {
- @Override
- public void dispatchMessage(Message msg) {
- int hours = seconds / 3600;
- int minutes = (seconds % 3600) / 60;
- int sec = seconds % 60;
- String time = String.format("%d:%02d:%02d", hours, minutes, sec);
- textStopwatch.setText(time);
- if (msg.what == START) {
- seconds++;
- if (isStarted) {
- h.sendEmptyMessageDelayed(START, 1000);
- }
- } else if (msg.what == STOP) {
- stopStopwatch();
- }
- }
- };
- Unbinder unbinder;
- public static StopwatchFragment newInstance() {
- StopwatchFragment fragment = new StopwatchFragment();
- return fragment;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_stopwatch, container, false);
- unbinder = ButterKnife.bind(this, root);
- startStopwatch();
- return root;
- }
- public void startStopwatch() {
- isStarted = true;
- h.sendEmptyMessage(START);
- }
- public void stopStopwatch() {
- isStarted = false;
- h.removeMessages(START);
- }
- @OnClick(R.id.text_stopwatch)
- public void onStopwatchClick() {
- if (isStarted) {
- stopStopwatch();
- } else {
- startStopwatch();
- }
- }
- @OnLongClick(R.id.text_stopwatch)
- public boolean onStopWatchLongClick() {
- stopStopwatch();
- seconds = 0;
- return true;
- }
- @Override
- public void onDestroyView() {
- unbinder.unbind();
- h.removeMessages(START);
- h.removeMessages(STOP);
- super.onDestroyView();
- }
- }
- ______________________________________________________________
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="sourceit.com.timer.MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/action_timer"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/action_timer" />
- <Button
- android:id="@+id/action_stopwatch"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/action_stopwatch" />
- </LinearLayout>
- <FrameLayout
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </FrameLayout>
- </LinearLayout>
- ______________________________________________________________________
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/activity_second"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin">
- <TextView
- android:id="@+id/text_timer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:textSize="80sp" />
- </RelativeLayout>
- _________________________________________________________________
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/activity_second"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin">
- <TextView
- android:id="@+id/text_stopwatch"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:textSize="50sp" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement