gpt4 book ai didi

java - 使用倒数计时器循环 [自动重启]

转载 作者:行者123 更新时间:2023-11-30 02:45:00 25 4
gpt4 key购买 nike

你好,我有一个计时器,我想在它结束后立即重新启动 4 次。

我想像这样使用 for 循环:for(int i = 0; i<=3; i++)

我的问题是,我不知道如何在我的代码中使用循环。我不知道在哪里添加 i 变量。

(我还添加了一个多次播放音频的媒体播放器。您可能会注意到,我使用了 2 个 cntdowntimer。它是一个 Intervalltimer,从 30 秒开始,自动切换到第二个定时器,从 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);
}
}
}

最佳答案

试试这个不需要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();

}




}

关于java - 使用倒数计时器循环 [自动重启],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25218138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com