Domanda

I am trying to create a button which is only clickable after for example 3 seconds. It should display a countdown, when 0 is reached, one can click the button and an action gets performed. For example when the Activity gets called, the button displays a "3" first and counts down to "0", only then one can click on the button.

Any ideas how to achieve this?

È stato utile?

Soluzione

you should use CountDownTimer for that:

timer = new CountDownTimer( 3000, 1000 ) {
  @Override public void onTick( long millisUntilFinished ) {
    button.setText( "count down " + millisUntilFinished );
  }
  @Override public void onFinish() { 
    button.setOnClickListener( onClickListener );
  }
};

somewhere down below:

timer.start()

Altri suggerimenti

  button.setEnabled(false);

  new CountDownTimer(3000, 1000) {

     public void onTick(long millisUntilFinished) {
         button.setText("Wait for " + (millisUntilFinished / 1000) + " seconds");
     }

     public void onFinish() {
         button.setEnabled(true);
         button.setText("Press me!");
     }
  }.start();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top