2011-03-28 59 views

回答

0

Here你會發現所有必要的信息。正如你所看到的,有各種方法來啓動你的計時器。只需使用適當的schedule(...)方法即可。爲了停止計時器,您可以使用cancel()方法,然後使用purge()方法。但請注意,cancel()方法會取消與此計時器關聯的所有任務。

0

你說的是Timer類嗎?

您可以使用schedule方法安排TimerTaskTimerTask#cancel取消它。

0

如果你想停止倒數計時器。

0
Use this Working Code. 
    package com.example.stopwatch; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.os.SystemClock; 
    import android.util.Log; 

    import android.widget.Button; 
    import android.widget.Chronometer; 
    import android.widget.CompoundButton; 

    import android.widget.ToggleButton; 

    public class StopWatch extends Activity { 
    Chronometer mChronometer; 

    Button button; 
    private long start = SystemClock.elapsedRealtime(); 
    private long stop = 0; 
    private long totalPauseTime = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

      try { 
       super.onCreate(savedInstanceState); 

       setContentView(R.layout.main); 


       final ToggleButton toggle = (ToggleButton)   findViewById(R.id.toggleButton1); 
       final Chronometer chrono = (Chronometer) findViewById(R.id.chronometer1); 

       final ToggleButton toggle2 = (ToggleButton)  findViewById(R.id.toggleButton2); 
       final Chronometer chrono2 = (Chronometer)   findViewById(R.id.chronometer2); 

     toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       try { 
        Log.i("[chrono]", String.format("start:%d stop:%d delta: %d",start, stop, start - stop)); 
        Log.i("[chrono]", "base:" +chrono.getBase()); 
        Log.i("[chrono]", "elapsed time:" +SystemClock.elapsedRealtime()); 

        if (isChecked) { 
         chrono.start(); 
         start = SystemClock.elapsedRealtime(); 
         chrono.setBase(start - totalPauseTime); 
        } else { 
         totalPauseTime += SystemClock.elapsedRealtime() - start; 
         chrono.stop(); 
        } 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

    } 
    }); 

    toggle2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
       try { 
        Log.i("[chrono]", String.format("start:%d stop:%d   delta: %d",start, stop, start - stop)); 
        Log.i("[chrono]", "base:" +chrono.getBase()); 
        Log.i("[chrono]", "elapsed time:" +SystemClock.elapsedRealtime()); 

         if (isChecked) { 
          chrono2.start(); 
          start = SystemClock.elapsedRealtime(); 
          chrono2.setBase(start - totalPauseTime); 
        } else { 
         totalPauseTime +=     SystemClock.elapsedRealtime() - start; 
         chrono2.stop(); 
        } 
       } catch (Exception e) { 


    enter code here 

     // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

    } 
    }); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    }  }