简体   繁体   中英

Android countDownTimer with Progressbar. Can't setProgress

I am struggling with wierd issue, I've made countdown timer with round progressbar. Sometimes as the time runs out, there is few pixels of progressbar left (orange between x and t), even though it reaches onFinish function. It's not a problem no more as I set countDownInterval to 10, but then showProgress function doesn't quite work, it enters else statement, but doesn't actually set progressbar to 0. interval-100-out 时间

    private void startCountingTime() {

    if (!counting) {
        timeLeftInMillis = maxTime * 1000;
        endTime = System.currentTimeMillis() + timeLeftInMillis;
    }
    if (maxTime != 0) {
        countDownTimer = new CountDownTimer(timeLeftInMillis, 100) {

            @Override
            public void onTick(long l) {
                String text = String.format(Locale.getDefault(), "%02d:%02d",
                        TimeUnit.MILLISECONDS.toMinutes(l) % 60,
                        TimeUnit.MILLISECONDS.toSeconds(l) % 60);
                timeLeftInMillis = l;
                showProgress(l, maxTime);
                counting = true;
                question.setText(exposePasswd ? text : currentQuestion);//displays the timer or question
            }

            @Override
            public void onFinish() {
                step = 4;
                counting = false;
                progressBar.setProgress(0);
                question.setText("Out of time! doubletap for next question");
            }
        }.start();
    } else {
        question.setText("tap to reveal, doubletap for next");
        exposePasswd = false;
    }
}

private void showProgress(long l, int maxTime) {
    maxTime = maxTime * 1000;
    int prog = (int) ((l * 100) / maxTime);

    if (exposePasswd) {
        //   progressBar.setVisibility(View.VISIBLE);
        if (progressBar.getProgress() == 0) {
            progressBar.setProgress(prog);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            progressBar.setProgress(prog, true);
        } else {
            progressBar.setProgress(prog);
        }
    } else {
        progressBar.setProgress(0);
        Log.wtf("idk","setted");
    }
}

Any ideas how to fix it?

Im not sure, why this is happening, but you could probably easily bypass this issue by adding:

progressbar.setVisibility(Visibility.GONE);

in the else statement and

progressbar.setVisibility(Visibility.VISIBLE); 

when the timer starts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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