how to change format of chronometer?

后端 未结 7 2345
攒了一身酷
攒了一身酷 2020-12-14 18:25

One of my problem is of changing the format of chronometer in android. i have the problem that chronometer shows its time in 00:00 format and i want it to come in 00:00:00 f

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 19:11

    I was dealing with the same issue. The easiest way to achieve this goal would be overriding updateText method of the Chronometer, but unfortunately it is a private method so I have done this in this way :

        mChronometer.setText("00:00:00");
        mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                CharSequence text = chronometer.getText();
                if (text.length()  == 5) {
                    chronometer.setText("00:"+text);
                } else if (text.length() == 7) {
                    chronometer.setText("0"+text);
                }
            }
        });
    

    I know that it would be better to write custom widget but for small project it can be suitable.

    Setting format on chronometer allows formatting text surrounding actual time and time is formatted using DateUtils.formatElapsedTime .

    Hope that helps.

提交回复
热议问题