Android-我尝试在每次单击按钮时重置计时器,并且当 5 秒未单击时,需要返回原始页面(活动)
我试图在每次单击按钮时重置计时器,如果 5 秒内没有单击,它应该返回到原始页面(活动)。
到目前为止的代码:
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(seconds==5000){timer.cancel();} //It crashed when I added this line
counter++;
seconds = 5000;
timer.schedule(new TimerTask() {
public void run() {
Intent x = new Intent(startClickActivity.this, ClickCountActivity.class);
startActivity(x);
}
}, seconds);
}
});
我不知道要使用哪种计时器或者是否应该使用线程。
I'm trying to reset my timer every time a button is clicked, and if not clicked within 5 seconds, it should go back to the original page (activity).
Code so far:
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(seconds==5000){timer.cancel();} //It crashed when I added this line
counter++;
seconds = 5000;
timer.schedule(new TimerTask() {
public void run() {
Intent x = new Intent(startClickActivity.this, ClickCountActivity.class);
startActivity(x);
}
}, seconds);
}
});
I don't know what kind of timer to use or if I should be using threads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该在你的班级内。
这在老地方。
我认为它应该工作得很好:)
顺便说一句,你的代码:
将始终取消/终止你的计时器,因为
seconds
变量是由你设置的并且始终为 5000。This should be inside your class.
And this in old place.
I think it should work well :)
By the way, your code:
Will always cancel/terminate your timer, because
seconds
variable is set by you and always is 5000.