Why isn't onChronometerTick being called?

ぃ、小莉子 提交于 2019-12-11 04:34:47

问题


My code never enters the onChronometerTick method. What am I doing incorrectly? Do I need to set the tick time somehow? I can't figure out a way to do that.

elapsedTimer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer chronometer) {
        countUp[0] = (SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000;
        elapsedTimeDynamic.setText("" + countUp);
    }
});
elapsedTimer.start();

回答1:


For anyone in the future who is having trouble using chronometer for an elapsed timer/stopwatch and can't figure it out, I ended up scrapping chronometer and using CountDownTimer, but using it in such a way that it would also count up:

final long startTime = System.currentTimeMillis();
elapsedTimer = new CountDownTimer(activityTime + 1000, 100) {
    @Override
    public void onTick(long l) {

        long elapsedTime = System.currentTimeMillis() - startTime;
        elapsedTime = elapsedTime/1000;
        elapsedTimeTextView.setText(String.format("%02d", (elapsedTime)));
    }

    @Override
    public void onFinish() {

    }
}.start();


来源:https://stackoverflow.com/questions/17598440/why-isnt-onchronometertick-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!