计时器格式变为0:0到00:00?

时间:2011-09-23 11:15:38

标签: android timer format

我想要这样的天文台明星,如果是0:5(M:S)我想这样00:05 这是我正在使用的代码:

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

我必须改变修改..

2 个答案:

答案 0 :(得分:2)

您可以使用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

答案 1 :(得分:0)

这是一个简单而智能的解决方案,适用于时间格式00:00:00 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();