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

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


当前回答

使用芬兰湾的科特林:

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()

其他回答

输出:01:30

new CountDownTimer(90000, 1000) {

            public void onTick(long duration) {
                //tTimer.setText("seconds remaining: " + millisUntilFinished / 1000);
                //here you can have your logic to set text to edittext resource id
                // Duration
                long Mmin = (duration / 1000) / 60;
                long Ssec = (duration / 1000) % 60;
                if (Ssec < 10) {
                    tTimer.setText("" + Mmin + ":0" + Ssec);
                } else tTimer.setText("" + Mmin + ":" + Ssec);
            }

            public void onFinish() {
                tTimer.setText("00:00");
            }

        }.start();

只需通过传递秒和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();
}

使用芬兰湾的科特林:

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()

如果您使用以下代码(如已接受的答案所述),

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
       //here you can have your logic to set text to edittext
    }

    public void onFinish() {
        mTextField.setText("done!");
    }

}.start();

如果不仔细清理引用,将导致使用此代码的活动实例的内存泄漏。

使用以下代码

//Declare timer
CountDownTimer cTimer = null;

//start timer function
void startTimer() {
    cTimer = new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
        }
        public void onFinish() {
        }
    };
    cTimer.start();
}


//cancel timer
void cancelTimer() {
    if(cTimer!=null)
        cTimer.cancel();
}

你需要调用cTtimer.cancel()每当onDestroy()/onDestroyView()在拥有的活动/片段被调用。

如CountDownTimer的文档所示:

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