android 计数器

来源:互联网 发布:cnpm 安装node sass 编辑:程序博客网 时间:2024/06/01 03:57
//时间计数器,最多只能到99小时,如需要更大小时数需要改改方法
     public String showTimeCount(long time) {
         if(time >= 360000000){
             return "00:00:00";
         }
         String timeCount = "";
         long hourc = time/3600000;
         String hour = "0" + hourc;
         hour = hour.substring(hour.length()-2, hour.length());
        
         long minuec = (time-hourc*3600000)/(60000);
         String minue = "0" + minuec;
         minue = minue.substring(minue.length()-2, minue.length());
        
         long secc = (time-hourc*3600000-minuec*60000)/1000;
         String sec = "0" + secc;
         sec = sec.substring(sec.length()-2, sec.length());
         timeCount = hour + ":" + minue + ":" + sec;
         return timeCount;
     }
    
     private Handler stepTimeHandler;
     private Runnable mTicker;
     long startTime = 0;
    
     //开始按钮
     class startBtnListener implements OnClickListener {
         @Override
         public void onClick(View v) {
             Button b = (Button)v;
             String buttonText = b.getText().toString();
             if("Start".equalsIgnoreCase(buttonText)){
                 b.setText("Stop");
                 // 清零 开始计时
                 stepTimeTV.setText("00:00:00");
                 stepTimeHandler = new Handler();
                 startTime = System.currentTimeMillis();
                 mTicker = new Runnable() {
                     public void run() {
                         String content = showTimeCount(System.currentTimeMillis() - startTime);
                         stepTimeTV.setText(content);
 
                         long now = SystemClock.uptimeMillis();
                         long next = now + (1000 - now % 1000);
                         stepTimeHandler.postAtTime(mTicker, next);
                     }
                 };
                 //启动计时线程,定时更新
                 mTicker.run();
             }else{
                 b.setText("Start");
                 //停止计时 Remove any pending posts of Runnable r that are in the message queue.
                 stepTimeHandler.removeCallbacks(mTicker);
             }
         }
     }
0 0
原创粉丝点击