0

我在一个应用程序中制作了计时器,我已经启动它并停止它并且无法在它暂停的同时暂停和恢复。我使用以下代码来实现应用程序的时间。

 String Current_click_btn ;

当单击暂停 btn 时,以毫秒为单位保存的上一个当前时间,在恢复时,我会将以前保存的值增加 100 值,因此当再次启动线程时,它将增加保存的当前时间 100. 重复

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
       if(v ==Start)
            {
                Current_clickbtn="Start";
                startTime = System.currentTimeMillis();
                mHandler.removeCallbacks(startTimer);
                mHandler.postDelayed(startTimer, 0);
            } 

            if(v == Pause)
            {
                Current_clickbtn="Pause";
                if(Pause.getText().equals("Pause"))
                {
                    Pause.setText("test");
                    mHandler.removeCallbacks(startTimer);
                }
                else
                {   
                    mHandler.postDelayed(startTimer, 0);
                    Pause.setText(getString(R.string.pause));
                }
            }


            }
   private Runnable startTimer = new Runnable() 
{
       public void run() {

          if(Current_clickbtn.equals("Start"))
          {
             CurrentTime = System.currentTimeMillis();
             elapsedTime = CurrentTime - startTime;
          }
          else
          {   
               elapsedTime = CurrentTime - startTime;
              CurrentTime+=100;
          }
             updateTimer(elapsedTime,ctx);

               mHandler.postDelayed(this,REFRESH_RATE);

       }
};

            public void updateTimer (float time,Context ctx){
                    secs = (long)(time/1000);
                    mins = (long)((time/1000)/60);
                    hrs = (long)(((time/1000)/60)/60);

                    /* Convert the seconds to String 
                     * and format to ensure it has
                     * a leading zero when required
                     */
                    secs = secs % 60;
                    seconds=String.valueOf(secs);
                    if(secs == 0){
                        seconds = "00";
                    }
                    if(secs <10 && secs > 0){
                        seconds = "0"+seconds;
                    }

                    /* Convert the minutes to String and format the String */

                    mins = mins % 60;
                    minutes=String.valueOf(mins);
                    if(mins == 0){
                        minutes = "00";
                    }
                    if(mins <10 && mins > 0){
                        minutes = "0"+minutes;
                    }

                    /* Convert the hours to String and format the String */

                    hours=String.valueOf(hrs);
                    if(hrs == 0){
                        hours = "00";
                    }
                    if(hrs <10 && hrs > 0){
                        hours = "0"+hours;
                    }

                    /* Although we are not using milliseconds on the timer in this example
                     * I included the code in the event that you wanted to include it on your own
                     */


                    System.out.println("***"+hours + ":" + minutes + ":" + seconds);
                    Timetextview=(TextView)((Activity) ctx).findViewById(com.carenet.activity.R.id.textView_time);
                    Timetextview.setText(hours + ":" + minutes + ":" + seconds);

                }

解放军帮助。

4

1 回答 1

0

您的代码如下:

                     CurrentTime = System.currentTimeMillis();
                     elapsedTime = CurrentTime - startTime;

也就是说,你是否暂停它,它显示时间startTime

于 2013-02-07T09:18:57.603 回答