2 slaks SLaks 于 2013.07.23 16:29 提问

使用计时器显示时间出现的问题

我在程序中使用定时器,我需要显示将来几天剩余的小时,分钟和秒。
我获取了时间,分钟和秒,但是当我在text view上设置时,计时器不启动。

Date date = new Date(2013,Integer.parseInt(datess.get(k).split("-")[1])-1,Integer.parseInt(datess.get(k).split("-")[0]),hours,mins,secs);  
     long dtMili = System.currentTimeMillis();  
     Date dateNow = new Date(dtMili);  
      remain = date.getTime() - dateNow.getTime();
MyCount counter = new MyCount(remain,1000);
            counter.start();
public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        }
    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        tv3.setText("done");
    }
    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        tv3.setText(timeCalculate(millisUntilFinished/1000) + " Countdown");
    }
}
 public String timeCalculate(long ttime)   
   {  
     long  daysuuu,hoursuuu, minutesuuu, secondsuuu;  
     String daysT = "", restT = "";  
     daysuuu = (Math.round(ttime) / 86400);  
     hoursuuu = (Math.round(ttime) / 3600) - (daysuuu * 24);  
     minutesuuu = (Math.round(ttime) / 60) - (daysuuu * 1440) - (hoursuuu * 60);  
     secondsuuu = Math.round(ttime) % 60;  
     if(daysuuu==1) daysT = String.format("%d day ", daysuuu);  
     if(daysuuu>1) daysT = String.format("%d days ", daysuuu);  
     restT = String.format("%02d:%02d:%02d", hoursuuu, minutesuuu, secondsuuu);  
     return daysT + restT;  
   }  

输出:
CSDN移动问答
为什么计时器没有开始?

1个回答

shaojie519
shaojie519   2013.07.23 17:03
已采纳

可以试下下边的方法

@Override
public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    tv3.post(new Runnable() {
                @Override
                public void run() {
                            txt3.setText(timeCalculate(millisUntilFinished/1000) + " Countdown"); 
                }
   });
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!