我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
当前回答
// the count down timer
new CountDownTimer(30000, 1000)
{
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
//on finish the count down timer finsih
}
}
}
}.start();
其他回答
如CountDownTimer的文档所示:
new CountDownTimer(30000, 1000) { onTick(long milliseconds untilfinished) { mTextField。setText("seconds remaining: " + msuntilfinished / 1000); //逻辑来设置EditText可以在这里 } onFinish() { mTextField.setText(“完成了!”); } } .start ();
有Rx的纯溶液,有人可能会感兴趣
数字扩展(Double, Float, Long等):
fun Number.countDownTimer(tick: Long = 1, timeUnit: TimeUnit = TimeUnit.SECONDS): Observable<Long> {
val count = this.toLong()
return Observable.interval(tick, timeUnit)
.take(count)
.map { count - it - 1 }
}
使用
60.countDownTimer().subscribe { textView.text = it }
只需通过传递秒和textview对象调用下面的函数
public void reverseTimer(int Seconds,final TextView tv){
new CountDownTimer(Seconds* 1000+1000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tv.setText("TIME : " + String.format("%02d", minutes)
+ ":" + String.format("%02d", seconds));
}
public void onFinish() {
tv.setText("Completed");
}
}.start();
}
我用kotlin流实现了一个很酷的定时器方法,所以你可以在ViewModel中实现它
var countDownInit = 30
fun countDownTimer() = flow<Int> {
var time = countDownInit
emit(time)
while (true){
time--
delay(1000L)
countDownInit = time
emit(time)
}
}
然后在你的activity或fragment中像这样调用这个函数
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.countDownTimer().collect{time->
//and update UI
//and for the finish section you can just use this
this.cancel()
}
}
}
现在在应用程序生命周期的暂停阶段会出现崩溃,你总是有最新的时间
接口方式。
import android.os.CountDownTimer;
/**
* Created by saikiran on 07-03-2016.
*/
public class CountDownTimerCustom extends CountDownTimer {
private TimeTickListener mTickListener;
private TimeFinishListener mFinishListener;
private long millisUntilFinished;
public CountDownTimerCustom(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void updateTickAndFinishListener(TimeTickListener tickListener) {
mTickListener = tickListener;
}
public void updateFinishListner(TimeFinishListener listener) {
mFinishListener = listener;
}
public long getCurrentMs() {
return millisUntilFinished;
}
public int getCurrentSec() {
return (int) millisUntilFinished / 1000;
}
@Override
public void onTick(long millisUntilFinished) {
this.millisUntilFinished = millisUntilFinished;
if (mTickListener != null)
mTickListener.onTick(millisUntilFinished);
}
@Override
public void onFinish() {
if (mTickListener != null)
mTickListener.onFinished();
mFinishListener.onFinished();
}
public interface TimeTickListener {
void onTick(long mMillisUntilFinished);
void onFinished();
}
public interface TimeFinishListener {
void onFinished();
}
}