我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。

此外,我如何才能保持它的更新,直到它达到零分零秒?


当前回答

public class Scan extends AppCompatActivity {
int minute;
long min;
TextView tv_timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan2);
    tv_timer=findViewById(R.id.tv_timer);
    minute=Integer.parseInt("Your time in string form like 10");
    min= minute*60*1000;
    counter(min);
}
private void counter(long min) {
    CountDownTimer timer = new CountDownTimer(min, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000) % 60;
            int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
            int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
            tv_timer.setText(String.format("%d:%d:%d", hours, minutes, seconds));
        }
        public void onFinish() {
            Toast.makeText(getApplicationContext(), "Your time has been completed",
                    Toast.LENGTH_LONG).show();
        }
    };
    timer.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()          
                 }
        }
}

现在在应用程序生命周期的暂停阶段会出现崩溃,你总是有最新的时间

public class Scan extends AppCompatActivity {
int minute;
long min;
TextView tv_timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan2);
    tv_timer=findViewById(R.id.tv_timer);
    minute=Integer.parseInt("Your time in string form like 10");
    min= minute*60*1000;
    counter(min);
}
private void counter(long min) {
    CountDownTimer timer = new CountDownTimer(min, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000) % 60;
            int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
            int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
            tv_timer.setText(String.format("%d:%d:%d", hours, minutes, seconds));
        }
        public void onFinish() {
            Toast.makeText(getApplicationContext(), "Your time has been completed",
                    Toast.LENGTH_LONG).show();
        }
    };
    timer.start();
}

}

使用芬兰湾的科特林:

var timer = object: CountDownTimer(30000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            tvTimer.setText("seconds remaining: " + millisUntilFinished / 1000)
        }

        override fun onFinish() {
            tvTimer.setText("done!")
        }
    }
timer.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 }

如CountDownTimer的文档所示:

new CountDownTimer(30000, 1000) { onTick(long milliseconds untilfinished) { mTextField。setText("seconds remaining: " + msuntilfinished / 1000); //逻辑来设置EditText可以在这里 } onFinish() { mTextField.setText(“完成了!”); } } .start ();