你好我有一个计时器,我想连续4次重新开始。
我想像这样使用for循环:for(int i = 0; i< = 3; i ++)
我的问题是,我不知道如何在我的代码中使用循环。 我不知道在哪里添加i变量。
(我还添加了一个多次播放音频的媒体播放器。你可能会注意到,我使用了2个cntdowntimers。它是一个以30秒开始的Intervalltimer,自动切换到第二个定时器,开始15秒,之后程序应该像我说的那样重新开始)
以下是我使用的方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("00:00:00");
mp = MediaPlayer.create(this, R.raw.vuvuneu);
btnStart = (Button)this.findViewById(R.id.btnStart);
counter=new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
...
}
@Override
public void onFinish() {
textViewTime.setText("Done");
mp.start();
}
};
final CounterClass timer = new CounterClass(30000,1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
mp.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter.start();
mp.start();
}
提前感谢。 :)
完整代码:
public class MainActivity extends ActionBarActivity {
CountDownTimer counter;
MediaPlayer mp;
Button btnStart, btnStop;
TextView textViewTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("00:00:00");
mp = MediaPlayer.create(this, R.raw.vuvuneu);
btnStart = (Button)this.findViewById(R.id.btnStart);
counter=new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
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);
textViewTime.setText(hms);
}
@Override
public void onFinish() {
textViewTime.setText("Done");
mp.start();
}
};
final CounterClass timer = new CounterClass(30000,1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
mp.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter.start();
mp.start();
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
textViewTime.setText("seconds remaining: " + millisUntilFinished / 1000);
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
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);
textViewTime.setText(hms);
}
}
}
答案 0 :(得分:1)
尝试这种简单的方法,不需要for循环
public class MainActivity extends Activity {
CountDownTimer workoutTimer;
CountDownTimer restTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startWorkoutTimer(0);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (workoutTimer!=null ){
workoutTimer.cancel();
}
if (restTimer!=null ){
restTimer.cancel();
}
}
});
}
public void startWorkoutTimer(final int count ){
workoutTimer=new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
if(count!=3){
startRestTimer(count);
}
}
};
workoutTimer.start();
}
public void startRestTimer(final int count ){
restTimer=new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
if(count!=3){
startWorkoutTimer(count+1);
}
}
};
restTimer.start();
}
}