Сделать базовый таймер для Android? - PullRequest
0 голосов
/ 23 февраля 2012

У меня есть макет main.xml с полями для установки времени для тренировки, и вы будете предупреждены, когда время истечет для каждого.Вы просто нажимаете кнопку запуска, чтобы запустить таймеры.Как сделать так, чтобы при вводе времени и нажатии «Пуск» он автоматически запускал синхронизацию и предупреждал вас?

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Warm-Up" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" >
</EditText>

<TextView
    android:id="@+id/textView4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fast" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Recovery" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" >
</EditText>

<TextView
    android:id="@+id/textView6"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Times" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Cool-Down" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start" />

Ответы [ 2 ]

2 голосов
/ 23 февраля 2012
 new CountDownTimer(timeToBlow, timeToTick) {

 public void onTick(long millisUntilFinished) {
     Log.d(TAG,"About to blow!!!");
 }

 public void onFinish() {
     Log.d(TAG,"BOOM!");
 }
}.start();

Просто замените timeToBlow временем, которое вы получаете из текстовых полей, а timeToTick - любым временем, которое вы хотите.

Источник

0 голосов
/ 23 февраля 2012

Вот базовый пример таймера Java, который я нашел, просто выполнив поиск в Google.http://www.gammelsaeter.com/programming/simple-timer-example-in-java/

Получите пользовательский ввод из вашего текстового поля и используйте его, чтобы установить время задержки.Что касается ваших предупреждений, вам придется запускать что-то из фонового потока, как только закончится таймер.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...