在我的应用中,我使用的是CountdownTimer
。在每次执行倒数计时器之后我使用计数器。当计数器满足某个要求时,我希望倒数计时器以另一种方式onFinish
运行并更改textTimer的textview。就像它现在行动一样,我的计数器在启动倒数计时器之前已经循环。我怎么能改变呢?
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
for(counter=0;counter<setsChosen;counter++) {
final CounterClass timer = new CounterClass(timeChosen * 60000, 1000);
timer.start();
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
}
}
public class CounterClass extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis= millisUntilFinished;
String hms=String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
@Override
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
if(counter<setsChosen){
this.start();
}
else
textTimer.setText("Complete");
}
}
}
答案 0 :(得分:1)
您可以移除自己的循环并在counter
更新onFinish()
所以,你的代码应该是这样的。
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
if (counter < setsChosen) {
final CounterClass timer = new CounterClass(timeChosen * 60000, 1000);
timer.start();
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
}
}
public class CounterClass extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis= millisUntilFinished;
String hms=String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
@Override
public void onFinish() {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
counter ++;
if(counter<setsChosen){
this.start();
}
else
textTimer.setText("Complete");
}
}
}
简单来说(不需要创建CountDownTimer的子类)。您可以像这样创建一个函数runCountDownTimer()
进行循环。
package org.example.anders.eazy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.os.Vibrator;
import java.util.concurrent.TimeUnit;
public class ExerciseActivity extends Activity {
String[] exerciseList;
TextView textTimer,workoutTextview,exerciseTextView;
int setsChosen;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exerciseList = getResources().getStringArray(R.array.heavy_chest_arms);
Intent intent = getIntent();
setsChosen =intent.getExtras().getInt("setsChosen");
String selectedWorkout = intent.getExtras().getString("workoutName");
int timeChosen = intent.getExtras().getInt("timeChosen");
setContentView(R.layout.activity_exercise);
textTimer = (TextView) findViewById(R.id.timeUntilFinished);
workoutTextview = (TextView) findViewById(R.id.workoutChosen);
//exerciseTextView= (TextView) findViewById(R.id.exerciseNow);
//textTimer.setText("00:01:00");
//exerciseTextView.setText(exerciseList[i]);
workoutTextview.setText(selectedWorkout);
counter = 0;
runCountDownTimer();
}
public void runCountDownTimer() {
if (counter < setsChosen) {
new CountDownTimer(timeChosen * 60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textTimer.setText(hms);
}
@Override
public void onFinish() {
counter++;
runCountDownTimer();
}
}.start();
}
}
}