Android Chronometer format

后端 未结 5 1356
囚心锁ツ
囚心锁ツ 2020-12-16 19:47

How can I set Android Chronometer format to HH:MM:SS??

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 20:11

    After some testing, I came up with this code. It's not fully tested but you can provide me with more info if you

    private void formatChronometerText(Chronometer c) {
        int cTextSize = c.getText().length();
        if (cTextSize == 5) {
            breakingTime.setFormat("00:%s");
        } else if (cTextSize == 7) {
            breakingTime.setFormat("0%s");
        } else if (cTextSize == 8) {
            breakingTime.setFormat("%s");
        }
    }
    

    I called this method in the onCreate() method eg.

    Chronometer c = ...
    ...
    formatChronometerText(c);
    c.setText("00:00:00");
    

    I'll be back in a day to verify if it works or it needs to be called also after the size of the text changes. If you are a precautious person I suggest that you call it in the same context with c.start() and c.stop()

    if(ticking){
        c.stop();
        formatChronometerText(c);
    } else {
        formatChronometerText(c);
        c.start()
    }
    

提交回复
热议问题