2015-11-10 121 views
0

我刨光很久以前這個代碼定時器Android應用程序啓動活動上創建

公共類MainActivity擴展活動{

private Button startButton;  
    private Button stopButton; 
    private Button clearButton; 
    private TextView timeValue;  
    private long timeStart = 0L; 
    private Handler timeHandler = new Handler(); 
    long timeInMilisec = 0L; 
    long timeMemo = 0L; 
    long timeUpdate = 0L; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    timeValue = (TextView) findViewById(R.id.timeValue);    

     startButton = (Button) findViewById(R.id.startButton);  
     startButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

       timeStart = SystemClock.uptimeMillis();  
       timeHandler.postDelayed(updateTimerThread, 0); 
      } 
     }); 

     stopButton = (Button) findViewById(R.id.stopButton);   
     stopButton.setOnClickListener(new View.OnClickListener() {  

      public void onClick(View view) {   
       timeMemo += timeInMilisec;  
       timeHandler.removeCallbacks(updateTimerThread); 
      } 
     }); 


    clearButton = (Button) findViewById(R.id.clearButton); 
    clearButton.setOnClickListener(new View.OnClickListener() {     
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.clearButton: 

       timeMemo = 0L;     
       timeValue.setText("00:00:00"); 
       int secs = 0;  
       int mins = 0;  
       secs = 0; 
       int milliseconds = 0; 
       timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
       timeUpdate = timeMemo + timeInMilisec;  
       timeValue.setText("" + mins + ":"  
         + String.format("%02d", secs) + ":"  
         + String.format("%03d", milliseconds)); 

       timeHandler.removeCallbacksAndMessages(updateTimerThread); 
       timeValue.setText("00:00:00"); 

       break;     
      }     
     } 
    }); 

    } 

    Runnable updateTimerThread = new Runnable() {  

     public void run() { 

      timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
      timeUpdate = timeMemo + timeInMilisec;  
      int secs = (int) (timeUpdate/1000);  
      int mins = secs/60;  
      secs = secs % 60;  
      int milliseconds = (int) (timeUpdate % 1000);  
      timeValue.setText("" + mins + ":"  
        + String.format("%02d", secs) + ":"  
        + String.format("%03d", milliseconds));  
      timeHandler.postDelayed(this, 0);    

     } 

    };  

現在我想我的計時器,當活動開始,而無需按啓動開始運行按鈕(我想刪除它) 我試圖把開始按鈕的代碼放在public void run()的末尾,但它沒有爲我工作。如何在啓動活動時啓動計時器?

回答

1

U可以模擬按下按鈕調用方法的onResume onPerformClick()()

@Override 
public void onResume(){ 
    startButton.onPerformClick(); 
} 
0

只要將代碼從startButtononClick直接進入您的onCreate()方法。像這樣...

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ////////////////////////////////////////////////////////// 
    ///Put your timer start code here /////////////////// 
    ///////////////////////////////////////////////////////// 
    timeStart = SystemClock.uptimeMillis();  
    timeHandler.postDelayed(updateTimerThread, 0); 
    ////////////////////////////////////////////////////////// 

    timeValue = (TextView) findViewById(R.id.timeValue);  

    stopButton = (Button) findViewById(R.id.stopButton);   
    stopButton.setOnClickListener(new View.OnClickListener() {  

     public void onClick(View view) {   
      timeMemo += timeInMilisec;  
      timeHandler.removeCallbacks(updateTimerThread); 
     } 
    }); 


    clearButton = (Button) findViewById(R.id.clearButton); 
    clearButton.setOnClickListener(new View.OnClickListener() {     
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.clearButton: 

       timeMemo = 0L;     
       timeValue.setText("00:00:00"); 
       int secs = 0;  
       int mins = 0;  
       secs = 0; 
       int milliseconds = 0; 
       timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
       timeUpdate = timeMemo + timeInMilisec;  
       timeValue.setText("" + mins + ":"  
         + String.format("%02d", secs) + ":"  
         + String.format("%03d", milliseconds)); 

       timeHandler.removeCallbacksAndMessages(updateTimerThread); 
       timeValue.setText("00:00:00"); 

       break;     
      }     
     } 
    }); 

    } 
相關問題