2014-11-25 70 views
0

我做了一個自定義秒錶顯示小時:minuted:seconds.Controls由START和STOP按鈕給出。但是,停止秒錶時出現問題。它並沒有停止時點擊STOP按鈕。任何人都請給我建議正確的解決方案。停止在Android的秒錶

package com.example.mystopwatch; 


import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 


public class StopwatchActivity extends Activity { 
private TextView textViewTimer; 
private Button BtnStart,BtnStop; 
private Handler mHandler = new Handler(); 
private long startTime; 
private long elapsedTime; 
private final int REFRESH_RATE = 100; 
private String hours,minutes,seconds,milliseconds; 
private long secs,mins,hrs,msecs; 
private boolean stopped = false; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stopwatch); 
    BtnStart=(Button)findViewById(R.id.startbtn); 
    BtnStop=(Button)findViewById(R.id.stopbtn); 
    BtnStart.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Runnable startTimer1 = new Runnable() 
      { 
       public void run() 
       { 
        elapsedTime = System.currentTimeMillis() - startTime; updateTimer(elapsedTime); 
        mHandler.postDelayed(this,REFRESH_RATE); 
        } 


       }; 
      if(stopped) 
      { 
       startTime = System.currentTimeMillis() - elapsedTime; 
       } 
      else 
      { 
       startTime = System.currentTimeMillis(); 
       } 
      mHandler.removeCallbacks(startTimer1); 
      mHandler.postDelayed(startTimer1, 0); 


     } 
       }); 
    BtnStop.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Runnable startTimer2 = new Runnable() 
       { 
        public void run() 
        { 
         elapsedTime = System.currentTimeMillis() - startTime; updateTimer(elapsedTime); 
         mHandler.postDelayed(this,REFRESH_RATE); 
         } 


        }; 

      mHandler.removeCallbacks(startTimer2); 
      stopped = true; 

     } 
    }); 

} 
private void updateTimer(long time) { 
    // TODO Auto-generated method stub 

     secs = (long)(time/1000); 
    mins = (long)((time/1000)/60); 
    hrs = (long)(((time/1000)/60)/60); 
    secs = secs % 60; 
    seconds=String.valueOf(secs); 
    if(secs == 0) 
    { 
     seconds = "00"; 

    } 
    if(secs <10 && secs > 0) 
    { 
     seconds = "0"+seconds; 
     } 
    mins = mins % 60; 
    minutes=String.valueOf(mins); 
    if(mins == 0){ minutes = "00"; 
    } 
    if(mins <10 && mins > 0) 
    { 
     minutes = "0"+minutes; 
     } 
    hours=String.valueOf(hrs); 
    if(hrs == 0) 
    { 
     hours = "00"; 
     } 
    if(hrs <10 && hrs > 0) 
    { 
     hours = "0"+hours; 
     } 
    milliseconds = String.valueOf((long)time); 
    if(milliseconds.length()==2) 
    { 
     milliseconds = "0"+milliseconds; 
     } 
    if(milliseconds.length()<=1) 
    { milliseconds = "00"; 
    } 
    milliseconds = milliseconds.substring(milliseconds.length()-3, milliseconds.length()-2); 
    ((TextView)findViewById(R.id.timertxt)).setText(hours + ":" + minutes + ":" + seconds); 
    } 
    } 
+0

添加您的代碼plz – 2014-11-25 06:22:08

+0

您是否試圖讓stopprd易失? – Fildor 2014-11-25 06:28:58

+0

該怎麼做? – sreenadh 2014-11-25 06:33:46

回答

0

您的BtnStop.setOnClickListener中的大部分代碼實際上是無用的。你定義了一個名爲startTimer2的Runnable,但是爲了什麼目的?除了致電mHandler.removeCallbacks(startTimer2);之外,您從不使用該Runnable。這將消除之前所做的任何呼叫,但由於您從未做過任何事情,所以這不起任何作用。

當您單擊停止按鈕時,您的方法執行的唯一操作是將布爾標誌stopped設置爲true。但是沒有定期檢查該標誌的過程。唯一一次檢查標誌是當按下START按鈕時。

作爲便箋,您使用REFRESH_RATE會造成混淆。由於postDelayed需要一個持續時間,因此設置較高的數字實際上會導致較低的刷新率。考慮重命名該變量或添加一個分區。

0

它不能停止,因爲您不在run方法中檢查boolean stopped。中移動它:

public void run() { 
    elapsedTime = System.currentTimeMillis() - startTime; 
    updateTimer(elapsedTime); 
    if(!stopped) { 
     mHandler.postDelayed(this, REFRESH_RATE); 
    } 
} 

,也考慮宣佈停止爲volatile按照建議的意見。

0

有整個任務啓動和停止,stopwatch只是調用: - handler.sendEmptyMessage(MSG_STOP_TIMER);停止秒錶

Handler handler = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 
     switch (msg.what) { 
     case MSG_START_TIMER: 
      timer.start(); //start timer 

      handler.sendEmptyMessage(MSG_UPDATE_TIMER); 
      break; 

     case MSG_UPDATE_TIMER: 
      //timerText.setText(String.format("%1$tM:%1$tS",timer.getElapsedTimeMin(),timer.getElapsedTimeSecs())); 
      if (timer.getElapsedTimeSecs() < 10) 
      { 
       if(timer.getElapsedTimeMin()<10){ 
        timerText.setText("0" + timer.getElapsedTimeMin() + ":0" + timer.getElapsedTimeSecs()); 
       }else{ 
        timerText.setText("" + timer.getElapsedTimeMin() + ":0" + timer.getElapsedTimeSecs()); 
       } 
      } 
      else{ 
       if(timer.getElapsedTimeMin()<10){ 
        timerText.setText("0" + timer.getElapsedTimeMin() + ":" + timer.getElapsedTimeSecs()); 
       }else{ 
        timerText.setText(timer.getElapsedTimeMin()+":"+ timer.getElapsedTimeSecs()); 
       } 

      } 
      handler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
      break;         //though the timer is still running 
     case MSG_STOP_TIMER: 
      handler.removeMessages(MSG_UPDATE_TIMER); // no more updates. 
      timer.stop();//stop timer 
      timerText.setText("0"+timer.getElapsedTimeMin()+":0"+ timer.getElapsedTimeSecs()); 
      break; 

     default: 
      break; 
     } 
    } 
};