There is a piece of code, you need to restart the timer when you press the button. That while he did not tick. Help, please, already tortured.

Timer:

timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable(){ @Override public void run(){; Rand(); } }); } }, 0, 2000); 

After pressing the button, the timer should restart:

 switch (v.getId()) { case R.id.button: if (button1.getText() == String.valueOf(n1 + n2)) { result++; //вот тут его надо перезагрузить Rand(); } 
  • Did you mean "restart"? Or something completely different? - Sergey Rufanov
  • Yes, that is what I meant. - Ker_ Jen

1 answer 1

Something like this:

  class MyTimerTask extends TimerTask{ public void run() { Intent x = new Intent(startClickActivity.this, ClickCountActivity.class); startActivity(x); } } // Your code MyTimerTask task = new MyTimerTask(); final long seconds = 5; timer.schedule(task,seconds*1000L); clicker.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { counter++; task.cancel(); task = new MyTimerTask(); timer.schedule(task,seconds*1000L); } }); 

You can do so:

 public class MainActivity extends Activity { TextView myCounter; Button btnStart; CountDownTimer countDownTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myCounter = (TextView) findViewById(R.id.mycounter); btnStart = (Button) findViewById(R.id.start); btnStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //cancel the old countDownTimer if(countDownTimer!=null){ countDownTimer.cancel(); } countDownTimer = new CountDownTimer(10000, 1000) { @Override public void onFinish() { myCounter.setText("Finished!"); } @Override public void onTick(long millisUntilFinished) { myCounter.setText("Millisecond Until Finished: " + String.valueOf(millisUntilFinished)); } }; countDownTimer.start(); } }); } } 

PS https://www.google.com/search?q=android+timer+with+restart