Android CountDown设置秒为edittext

时间:2018-08-28 01:17:31

标签: java android

我只能使用编辑文本设置倒数分钟,如何在倒数计时器上设置编辑秒数,任何帮助人员都不知道如何设置秒数

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;
        }


    }

2 个答案:

答案 0 :(得分:1)

您可以尝试显示分钟和秒:

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);
        }
 });

根据需要更改countDownTimer的初始化

答案 1 :(得分:0)

IMO,startTimer接受毫秒作为其参数。您无法在那里设置秒数计时器。但这可以显示秒数:

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

这以秒为单位显示计时器。

希望有帮助。