使用 android studio 我有一个带有CountDownTimer
. 当我按下一个按钮时,它从 10 秒开始到 0 秒,并且TextView
计算我在一个按钮上点击了多少次,到目前为止一切都很好。我想通过按另一个按钮重新启动来重新启动此活动。你能帮助我吗?如果有帮助,我把代码。
@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) { }
}