Chronometer with H:MM:SS

后端 未结 3 1316
陌清茗
陌清茗 2021-01-12 16:15

How to display Chronometer with H:MM:SS? I read MM:SS and H:MM:SS are displaying by default.I found only for MM:SS.

Here is my code for MM:SS with start and stop but

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 17:09

    You can also set with attractive Chronometer format like 00 hrs : 00 min : 00 sec

            chronometer.setText("00 hrs : 00 min : 00 sec");
            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    
                @Override
                public void onChronometerTick(Chronometer chronometer) {
                    CharSequence text = chronometer.getText();
                    if (text.length() == 5) {
                        String a[] = text.toString().split(":");
                        String min = a[0] + " min : ";
                        String sec = a[1] + " sec";
                        chronometer.setText("00 hrs : " + min + sec);
                    } else if (text.length() == 7) {
                        String a[] = text.toString().split(":");
                        String hr = a[0] + " hrs : ";
                        String min = a[1] + " min : ";
                        String sec = a[2] + " sec";
                        chronometer.setText("0" + hr + min + sec);
                    }
                }
            });
    

提交回复
热议问题