2015-09-11 100 views
-2

我打算在達到點擊30次點擊次數時將秒數增加5秒,然後當我達到60次時將秒數增加其他5次點擊等。如何增加計時器計數(CountDownTimer)

請幫助解決這個問題

public class game extends linear_layout { 
LinearLayout b1; 
TextView t1; 
int num=0; 
TextView timer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gamelayout); 
    timer = (TextView)findViewById(R.id.textView3); 
    b1 = (LinearLayout)findViewById(R.id.background); 
    t1 = (TextView)findViewById(R.id.textView2); 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int x = 10000; 
      num = num + 1; 
      t1.setText(Integer.toString(num)); 


      new CountDownTimer(x, 1000) { 

       public void onTick(long millisUntilFinished) { 
        timer.setText("" + millisUntilFinished/1000); 

       } 

       public void onFinish() { 

        Intent i1 = new Intent(game.this, linear_layout.class); 
        startActivity(i1); 
        finish(); 
       } 

     } 
        .start(); 
     } }); } } 

回答

0

無法更新的CountDownTimer的秒數,而不是當你想增加它,你只取消前一個,並創建與新CountDownTimer新的價值。

幾點需要注意:

不要在匿名的方式做new CountDownTimer,你需要的CountDownTimer一個參考,所以你就可以阻止它需要更新的時候。

的另一件事是,你不能從whithin其方法取消CountDownTimer,所以如果你想取消先前的計時器,並創建從onTick新CountDownTimer,你應該使用替代像CountDownTimer alternative

+0

是可能告訴我在哪裏指示新的CountDownTimer? ,我仍然是一個初學者大聲笑 –

+0

這取決於你的應用程序的邏輯,如果你會解釋更多,我會盡力幫助。 –

+0

它關於點擊..會有一個高分,如果我點擊超過高分的時間會增加5秒,那就是它 –