2017-10-13 6 views
0

Essayez d'implémenter un CountDownTimer qui devrait être affiché sur un bouton. Cela fonctionne bien tant que je ne tourne pas le smartphon. Après avoir tourné le smartphone, le bouton affiche uniquement le texte de la valeur initiale même si le cowndowntimer est toujours en cours d'exécution. Est-ce que quelqu'un sait pourquoi la méthode myButton.setText() ne fonctionne pas dans la méthode onTick() après avoir allumé le smartphone?Pourquoi ne puis-je pas dessiner le texte sur un bouton après avoir tourné le smarphone et utilisé un CountDownTimer?

public class MainActivity extends AppCompatActivity { 

    private TextView mTextMessage; 
    private TextView myTextView; 
    private Button myButton; 
    private CountDownTimer countDownTimer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mTextMessage = (TextView) findViewById(R.id.message); 
     myTextView= (TextView) findViewById(R.id.textView); 
     myButton= (Button) findViewById(R.id.button); 
     myTextView.setText("Set new text"); 
     myButton.setText("Set Button text"); 
     myButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       myButton.setText("Button clicked"); 
       countDownTimer = new CountDownTimer(60 * 1000, 1000) { 
        @Override 
        public void onTick(long millisUntilFinished) { 
     //After turning the Smartphone the follow both methods do not work anymore 
         myButton.setText("Calc: " + millisUntilFinished/1000); 
         myTextView.setText("Calc: " + millisUntilFinished/1000); 
        } 
        @Override 
        public void onFinish() { 
        } 
       }; 
       //timer started 
       countDownTimer.start(); 
      } 
     }); 
    } 
} 
+0

S'il vous plaît, vérifiez si le OnFinish est appelé quand vous avez tourné la dispositif. – AndroidStorm

+0

Finir n'est pas appelé après avoir tourné le Smartphone. Je peux également attacher le débogueur et voir que la minuterie est toujours en marche après avoir tourné. Mais le myButton.setText ("Calc:" + millisUntilFinished/1000); La méthode n'affiche pas la valeur. –

+0

En général, je ne pense pas que vous serez satisfait de ce code. Par exemple: avez-vous vu ce qui se passe lorsque vous appuyez sur le bouton une seconde ou une troisième fois, disons 5 secondes après le tapotement initial? – Barns

Répondre

0

Voici mon exemple de code ... Notez que j'ai ajouté le texte « DONE » dans la méthode OnFinish(), de sorte que vous pouvez définir le texte pour tous les composants de l'interface que vous voulez ... et aussi je ajouté une instruction if dans le cas où l'application est en cours d'exécution en arrière-plan dans le cas où vous souhaitez enregistrer des informations avant l'application fonctionne en arrière-plan

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 

    myButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myButton.setText("Button clicked"); 
      countDownTimer = new CountDownTimer(3000, 1000) { 
       @Override 
       public void onTick(long millisUntilFinished) { 
        //After turning the Smartphone the follow both methods do not work anymore 
        if (!runningBackground) { 
         myButton.setText("Calc: " + millisUntilFinished/1000); 
         myTextView.setText("Calc: " + millisUntilFinished/1000); 
        } else { 
         //Do something 
        } 
       } 
       @Override 
       public void onFinish() { 
        if (!runningBackground) { 
         //Do something 
        } 
        mTextMessage.setText("DONE"); 
        runningBackground = false; 
        running = false; 
       } 
      }; 
      //timer started 
      countDownTimer.start(); 
      running = true; 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    runningBackground = false; 
} 

@Override 
protected void onPause() { 
    runningBackground = true; 
    super.onPause(); 
}