[英]How can I increase the timer count ( CountDownTimer )
我计划在点击次数达到 30 次时将秒数增加 5 秒,然后在达到 60 次点击时将秒数增加 5 秒,依此类推。
请帮助解决这个问题
public class game extends linear_layout {
LinearLayout b1;
TextView t1;
int num=0;
TextView timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamelayout);
timer = (TextView)findViewById(R.id.textView3);
b1 = (LinearLayout)findViewById(R.id.background);
t1 = (TextView)findViewById(R.id.textView2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int x = 10000;
num = num + 1;
t1.setText(Integer.toString(num));
new CountDownTimer(x, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
Intent i1 = new Intent(game.this, linear_layout.class);
startActivity(i1);
finish();
}
}
.start();
} }); } }
您无法更新 CountDownTimer 的秒数,相反,当您想增加它时,您只需取消前一个并使用新值创建一个新的 CountDownTimer。
需要注意的几点:
不要以匿名方式创建new CountDownTimer
,您确实需要对 CountDownTimer 的引用,以便在需要更新时停止它。
另一件事是你不能从它的方法中取消一个 CountDownTimer,所以如果你想取消前一个计时器并从 onTick 创建新的 CountDownTimer,你应该使用一个替代方案,如CountDownTimer 替代方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.