2017-01-04 8 views
0

私のアプリでは、ユーザーが5秒以内にボタンをクリックするとポイントを取得します。その後、タイマーをキャンセルして再起動する必要があります。私が持っている問題は、タイマーcountinuesが再起動するまでカウントダウンしているということです。それは間違って設定されていますか、または​​は全くタイマーを停止していませんか?アンドロイド - カウントダウンタイマーがキャンセルされていません

public class MainActivity extends AppCompatActivity { 

    Button btn; 
    TextView text; 
    TextView scoretv; 
    private static final String FORMAT = "%02d:%02d"; 
    public int score = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     scoretv = (TextView) findViewById(R.id.textView3); 
     btn = (Button) findViewById(R.id.button1); 
     text = (TextView) findViewById(R.id.textView2); 
    } 


    public void onClick(View view) { 
     score++; 
     scoretv.setText(String.valueOf(score)); 
     load(); 
    } 

    private void load() { 
     // TODO Auto-generated method stub 

     new CountDownTimer(5000, 10) { // adjust the milli 
      // seconds here 
      public void onTick(long millisUntilFinished) { 
       text.setText("" 
         + String.format(
         "%02d:%03d", 
         TimeUnit.MILLISECONDS 
           .toSeconds(millisUntilFinished) 
           - TimeUnit.MINUTES 
           .toSeconds(TimeUnit.MILLISECONDS 
             .toMinutes(millisUntilFinished)), 
         TimeUnit.MILLISECONDS 
           .toMillis(millisUntilFinished) 
           - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS 
           .toSeconds(millisUntilFinished)))); 

      } 

      public void onFinish() { 
       text.setText("GameOver."); 
       cancel(); 
      } 
     }.start(); 
    } 
} 

答えて

3

あなたはonFinishcancel()を呼び出しているので、ユーザーがボタンをクリックすると、タイマーは停止しません。その代わりに、ボタンが5秒で開始され、CountDownTimerが開始し、タイマーの最後にcancel()が呼び出されます。しかし、既に終了したタイマーをキャンセルするのはどういう意味ですか?

この問題を解決するには、CountDownTimerオブジェクトのグローバルインスタンスを作成し、onCreateメソッドでインスタンス化し、onClickメソッドでキャンセルすることをお勧めします。

まず、あなたが最初にあなたのonCreateload方法で前に持っていたものを追加、その後、あなたのグローバルスコープに

CountDownTimer timer; 

これを追加し、

timer = new CountDownTimer(5000, 10) { // adjust the milli 
     // seconds here 
     public void onTick(long millisUntilFinished) { 
      text.setText("" 
        + String.format(
        "%02d:%03d", 
        TimeUnit.MILLISECONDS 
          .toSeconds(millisUntilFinished) 
          - TimeUnit.MINUTES 
          .toSeconds(TimeUnit.MILLISECONDS 
            .toMinutes(millisUntilFinished)), 
        TimeUnit.MILLISECONDS 
          .toMillis(millisUntilFinished) 
          - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS 
          .toSeconds(millisUntilFinished)))); 

     } 

     public void onFinish() { 
      text.setText("GameOver."); 
      //cancel(); <-this is redundant 
     } 
    }.start(); 

そして、あなたのonClick方法でtimer.cancel()を呼び出します、

public void onClick(View view) { 
    score++; 
    scoretv.setText(String.valueOf(score)); 
    //load(); <-unnecessary 
    timer.cancel(); 
} 

最後に、loadを取り除くことをお勧めします。

+0

新しいカウントダウンの問題にもお手伝いできますか? –

+0

@Joe S、これは何ですか? –

+0

私はそのために新しい質問を投稿しました - 私のタイマーは実際にキャンセルして再起動しないようです –

関連する問題