簡體   English   中英

Android CountDown 設置秒來編輯文本

[英]Android CountDown Set Seconds to edittext

我只能使用編輯文本在我的倒計時上設置分鍾,它如何還可以在倒數計時器上的編輯文本上設置秒數,任何幫助人員都不知道如何設置秒數

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


    }

你可以試試這個來顯示分鍾和秒:

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的初始化

IMO,startTimer 接受毫秒作為其參數。 您不能在那里以秒為單位設置計時器。 但是用它來顯示秒:

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

這以秒為單位顯示計時器。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM