Итак, я создаю приложение для тренировки, в котором я должен изменить текст на кнопке через определенное количество времени, чтобы представить новый набор. После нажатия кнопки «Пуск» я хочу, чтобы таймер запускался автоматически до тех пор, пока все наборы упражнений не будут выполнены. Если нажать кнопку «Пауза», текущий таймер будет остановлен до тех пор, пока кнопка пуска не будет нажата снова. Мне было интересно, как это сделать. Я пытался сделать это в цикле for, создавая новый таймер каждый раз, но он остановился после одного прогона. Есть идеи по этому поводу? У меня было хотя об использовании Handler для этого, но я не знаю, как это сделать.Автоматический таймер обратного отсчета по кнопке Нажмите
package com.dwolford.workoutroutine;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Vertical extends ActionBarActivity {
Handler handler = new Handler();
Button jumpRope;
Button start;
Button pause;
Button back;
EditText timeLeft;
String[] verticalWorkouts = {"JUMP ROPE", "STRETCH", "JUMP ROPE", "STRETCH",
"SLOW MOTION SQUATS", "STRETCH", "LATERAL JUMPS", "STRETCH", "ALTERNATING JUMP LUNGES", "STRETCH", "TUCK JUMPS", "STRETCH", "TOE RAISES", "STRETCH",
"SLOW MOTION SQUATS", "STRETCH", "LATERAL JUMPS", "STRETCH", "ALTERNATING JUMP LUNGES", "STRETCH", "TUCK JUMPS", "STRETCH", "TOE RAISES", "STRETCH",
"SLOW MOTION SQUATS", "STRETCH", "LATERAL JUMPS", "STRETCH", "ALTERNATING JUMP LUNGES", "STRETCH", "TUCK JUMPS", "STRETCH", "TOE RAISES", "STRETCH"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vertical);
jumpRope = (Button)findViewById(R.id.jumpRope);
jumpRope.setFocusableInTouchMode(true);
jumpRope.requestFocus();
timeLeft = (EditText)findViewById(R.id.time_left);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < verticalWorkouts.length; i++)
{
final int iTemp = i;
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
timeLeft.setText("secs: " + millisUntilFinished/1000);
}
int temp = iTemp;
public void onFinish() {
timeLeft.setText("done!");
jumpRope.setText(verticalWorkouts[temp]);
}
}.start();
}
}
});
pause = (Button)findViewById(R.id.pause);
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jumpRope.setText(verticalWorkouts[2]);
}
});
back = (Button)findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_vertical, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Вот XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WORKOUT: VERTICAL"
android:id="@+id/title"
android:height="50dp"
android:textSize="22dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toe Raises"
android:state_pressed="true"
android:id="@+id/toeRaises"
android:layout_below="@+id/tuckJumps"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tuck Jumps"
android:id="@+id/tuckJumps"
android:layout_below="@+id/altJumpLunge"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alternating jump lunges"
android:id="@+id/altJumpLunge"
android:layout_below="@+id/latJump"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lateral jumps"
android:id="@+id/latJump"
android:layout_below="@+id/button7"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slow motion squats"
android:id="@+id/button7"
android:layout_below="@+id/stretch"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stretch"
android:id="@+id/stretch"
android:layout_below="@+id/jumpRope"
android:background="@drawable/button_gradient"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUMP ROPE"
android:id="@+id/jumpRope"
android:layout_below="@+id/title"
android:layout_alignParentLeft="true"
android:background="@drawable/button_gradient"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TIME:"
android:id="@+id/time"
android:height="50dp"
android:textSize="22dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:layout_alignBottom="@+id/jumpRope"
android:layout_alignRight="@+id/title"
android:layout_alignEnd="@+id/title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sets Left: "
android:id="@+id/setsLeft"
android:height="50dp"
android:textSize="22dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:layout_below="@+id/toeRaises"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:id="@+id/pause"
android:background="@drawable/button_gradient"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:id="@+id/start"
android:layout_alignTop="@+id/pause"
android:background="@drawable/button_gradient"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:id="@+id/back"
android:background="@drawable/button_gradient"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/toeRaises"
android:layout_toRightOf="@+id/toeRaises"
android:layout_alignRight="@+id/latJump"
android:layout_alignEnd="@+id/latJump" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time_left"
android:layout_below="@+id/time"
android:layout_alignRight="@+id/back"
android:layout_alignEnd="@+id/back"
android:layout_alignLeft="@+id/time"
android:layout_alignStart="@+id/time" />
Если вы ответили на вопрос, убедитесь, что вы отметили ответ как ответ. – Remian8985