2013-10-16 183 views
0

與android工作室我有一個應用程序與CountDownTimer。當我按下一個按鈕和一個TextView來計算我在一個按鈕上做了多少點擊,它從10秒開始到0,到目前爲止非常好。我想通過按下另一個按鈕重新啓動來重新啓動此活動。你可以幫我嗎?如果它幫助我把代碼。重新啓動CountDownTimer

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

    txtCount = (TextView)findViewById(R.id.textView1); 
    txtCount.setText(String.valueOf(count)); 
    btnCount = (Button)findViewById(R.id.button1); 
    btnRestart = (Button)findViewById(button2); 

    final boolean[] timerProcessing = {false}; 
    final boolean[] timerStarts = {false}; 

    final TextView textViewTimer = (TextView)findViewById(R.id.textView2); 
    //Saving link to timer object 
    final CountDownTimer timer = new CountDownTimer(10000, 1) { 

     public void onTick(long millisUntilFinished) { 
      textViewTimer.setText("" + millisUntilFinished/1000 
        + ":" + millisUntilFinished % 1000); 
         } 


     public void onFinish() { 
      textViewTimer.setText("0:000"); 
      timerProcessing[0] = false; 
     } 

    }; 

    btnCount.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      //start timer once when button first click 
      if (!timerStarts[0]){ 
       timer.start(); 
       timerStarts[0] = true; 
       timerProcessing[0] = true; 
      } 

      if (timerProcessing[0]){ 
       count++; 
       txtCount.setText(String.valueOf(count)); 
      } 
     } 
    }); 
    btnRestart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { } 
} 

回答

2

只需更換下面的代碼:

private TextView txtCount, textViewTimer; 
private Button btnCount, btnRestart; 
int count = 0; 
boolean[] timerProcessing = { false }; 
boolean[] timerStarts = { false }; 
private MyCount timer; 

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

    txtCount = (TextView) findViewById(R.id.textView1); 
    txtCount.setText(String.valueOf(count)); 
    btnCount = (Button) findViewById(R.id.button1); 
    btnRestart = (Button) findViewById(R.id.button2); 

    textViewTimer = (TextView) findViewById(R.id.textView2); 

    timer = new MyCount(10000, 1); 

    btnCount.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      // start timer once when button first click 
      if (!timerStarts[0]) { 
       timer.start(); 
       timerStarts[0] = true; 
       timerProcessing[0] = true; 
      } 

      if (timerProcessing[0]) { 
       count++; 
       txtCount.setText(String.valueOf(count)); 
      } 
     } 
    }); 
    btnRestart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      timerProcessing[0] = true; 
      count = 0; 
      txtCount.setText(String.valueOf(count)); 
      timer.cancel(); 
      timer.start(); 
     } 
    }); 
} 

public class MyCount extends CountDownTimer { 
    public MyCount(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
     textViewTimer.setText("0:000"); 
     timerProcessing[0] = false; 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     textViewTimer.setText("" + millisUntilFinished/1000 + ":" 
       + millisUntilFinished % 1000); 

    } 
} 

這裏您計數器varibale被替換爲一個內部類,這樣你不需要創建計數器變量每一次。如果你想重新啓動計數器,只需創建一次計數器變量並調用它的啓動方法。

0

爲什麼您必須重新啓動活動。你需要有一個邏輯,並處理重啓按鈕onclicklistener。

將您的倒計時代碼移動到一個函數中,並從您的活動onCreate/onResume中調用此函數,也可以從重新啓動按鈕中單擊。

1

CountDownTimer包裝在一個方法中,然後在您單擊重新啓動按鈕時再次調用它。