开发者

Chronometer format change to 0:0 to 00:00?

开发者 https://www.devze.com 2023-04-07 15:24 出处:网络
i want chronometer star like this, if it is at 0:5 (M:S) i want like this 00:05 this is the code i am using :

i want chronometer star like this, if it is at 0:5 (M:S) i want like this 00:05 this is the code i am using :

stopWatch = (Chronometer) findViewById(R.id.chrono);
    stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
        开发者_C百科public void onChronometerTick(Chronometer arg0) {
            countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
            String asText = (countUp / 60) + ":" + (countUp % 60); 
            time_view.setText("Time : " +asText);
        }
    });
    stopWatch.start();

where i have to change for that modification..


You could use NumberFormatter

NumberFormat formatter = new DecimalFormat("00");
stopWatch = (Chronometer) findViewById(R.id.chrono);
stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
    @Override
    public void onChronometerTick(Chronometer arg0) {
        countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
        String asText = formatter.format(countUp / 60) + ":" + formatter.format(countUp % 60); 
        time_view.setText("Time : " +asText);
    }
});
stopWatch.start();

Some more number format examples


Here is an easy and smart solution for time format 00:00:00 in chronometer in android

Chronometer timeElapsed  = (Chronometer) findViewById(R.id.chronomete);
timeElapsed.setOnChronometerTickListener(new OnChronometerTickListener(){
    @Override
        public void onChronometerTick(Chronometer cArg) {
        long time = SystemClock.elapsedRealtime() - cArg.getBase();
        int h   = (int)(t/3600000);
        int m = (int)(t - h*3600000)/60000;
        int s= (int)(t - h*3600000- m*60000)/1000 ;
        String hh = h < 10 ? "0"+h: h+"";
        String mm = m < 10 ? "0"+m: m+"";
        String ss = s < 10 ? "0"+s: s+"";
        cArg.setText(hh+":"+mm+":"+ss);
    }
});
timeElapsed.setBase(SystemClock.elapsedRealtime());
timeElapsed.start();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号