Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置一个计时器,当它在一个活动中完成某件事情后,如果点击按钮,其他事情就会发生?_Java_Android - Fatal编程技术网

Java 如何设置一个计时器,当它在一个活动中完成某件事情后,如果点击按钮,其他事情就会发生?

Java 如何设置一个计时器,当它在一个活动中完成某件事情后,如果点击按钮,其他事情就会发生?,java,android,Java,Android,我的android应用程序中有一个向用户显示单词的活动。如果用户能够在不到60秒内猜出答案,他将按下按钮并转到另一个活动。但是,如果他不能做到这一点,时间结束了,第三个活动必须出现。我该怎么做?用线程或定时器之类的东西 我尝试过线程化,但应用程序崩溃。您可以通过使用处理程序来实现这一点 Kotlin // declare this variables as attributes in you class val handler = Handler() val runnable = Runnabl

我的android应用程序中有一个向用户显示单词的活动。如果用户能够在不到60秒内猜出答案,他将按下按钮并转到另一个活动。但是,如果他不能做到这一点,时间结束了,第三个活动必须出现。我该怎么做?用线程或定时器之类的东西


我尝试过线程化,但应用程序崩溃。

您可以通过使用处理程序来实现这一点

Kotlin

// declare this variables as attributes in you class
val handler = Handler()
val runnable = Runnable {
    // Call something when it finishes
}
handler.postDelayed(runnable, 60_000) // Do something after 60 seconds


// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable)
Java

// declare this variables as attributes in you class
Handler handler = new Handler();
Runnable runnable = new Runnable() {
    public void run() {
        // Call something when it finishes
    }
};
handler.postDelayed(runnable, 60_000);

// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable);

问题标签中的问题对我来说不清楚发生了什么,应该做什么。请重新措辞。另外,发布您的代码,这样我们就可以了解线程问题,或许可以在这方面提供帮助?@ZF007。我是新来的,我不知道如何编辑我的问题。我搜索了一个编辑按钮或类似的东西,但没有找到。我接受你的评论,这是因为我的英语周。虽然我已经找到了解决方案,但要感谢william xyz。我也感谢你的反馈。效果非常好!非常感谢你,威廉。