與android工作室我有一個應用程序與CountDownTimer
。當我按下一個按鈕和一個TextView
來計算我在一個按鈕上做了多少點擊,它從10秒開始到0,到目前爲止非常好。我想通過按下另一個按鈕重新啓動來重新啓動此活動。你可以幫我嗎?如果它幫助我把代碼。重新啓動CountDownTimer
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);
btnRestart = (Button)findViewById(button2);
final boolean[] timerProcessing = {false};
final boolean[] timerStarts = {false};
final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
//Saving link to timer object
final CountDownTimer timer = new CountDownTimer(10000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished/1000
+ ":" + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setText("0:000");
timerProcessing[0] = false;
}
};
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//start timer once when button first click
if (!timerStarts[0]){
timer.start();
timerStarts[0] = true;
timerProcessing[0] = true;
}
if (timerProcessing[0]){
count++;
txtCount.setText(String.valueOf(count));
}
}
});
btnRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { }
}