简体   繁体   English

Android CountDown 设置秒来编辑文本

[英]Android CountDown Set Seconds to edittext

I can set only minutes on my countdown using edit text, how about it can set also seconds on edit text on the countdown timer, any help guys don't have any clue how to set seconds我只能使用编辑文本在我的倒计时上设置分钟,它如何还可以在倒数计时器上的编辑文本上设置秒数,任何帮助人员都不知道如何设置秒数

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startTimer:
                //If CountDownTimer is null then start timer
                if (countDownTimer == null) {
                    String getMinutes = minutes.getText().toString();//Get minutes from edittexf
                    //Check validation over edittext
                    if (!getMinutes.equals("") && getMinutes.length() > 0) {
                        int noOfMinutes = Integer.parseInt(getMinutes) * 60 * 1000;//Convert minutes into milliseconds

                        startTimer(noOfMinutes);//start countdown
                        startTimer.setText(getString(R.string.stop_timer));//Change Text

                    } else
                        Toast.makeText(MainActivity.this, "Please enter no. of Minutes.", Toast.LENGTH_SHORT).show();//Display toast if edittext is empty
                } else {
                    //Else stop timer and change text
                    stopCountdown();
                    startTimer.setText(getString(R.string.start_timer));
                }
                break;
            case R.id.resetTimer:
                stopCountdown();//stop count down
                startTimer.setText(getString(R.string.start_timer));//Change text to Start Timer
                countdownTimerText.setText(getString(R.string.timer));//Change Timer text
                break;
        }


    }

You can try this to show minutes and seconds:你可以试试这个来显示分钟和秒:

new CountDownTimer(90000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String text = String.format(Locale.getDefault(), "Time Remaining %02d min: %02d sec", 
            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, 
            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
            tvTime.setText(text);
        }
 });

Change the initialization of countDownTimer according to your needs根据需要更改countDownTimer的初始化

IMO, startTimer accepts milliseconds as its parameters. IMO,startTimer 接受毫秒作为其参数。 You cannot set the Timer in seconds there.您不能在那里以秒为单位设置计时器。 But use this to show seconds:但是用它来显示秒:

startTimer.setText(Integer.parseInt(getString(R.string.stop_timer)) / 1000);

This shows the timer in Seconds.这以秒为单位显示计时器。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM