Android 事件计时(Toast 和 Intents)

发布于 2024-11-10 09:11:17 字数 643 浏览 8 评论 0原文

我正在尝试显式启动新活动的意图,但我希望当前活动中的一些代码首先完成执行。我做了一些研究,并且有一些想法来实现这一目标,但我认为“必须有一种更简单的方法来做到这一点”。这是相关的代码块:

        cpuToast(dmg);
        if (player_.getStatus() == false)
        {
            playerWon_ = false;
            Intent intent = new Intent(Main.this, Death.class);
            startActivity(intent);
        }

dmg 是一个 int。 cpuToast 只是创建一个字符串来显示损坏情况,然后调用 show()。 getStatus() 返回玩家是否被杀死。如果玩家被杀,我会启动一个新的意图,它将播放玩家死亡的动画。不幸的是,“死亡”活动在 Toast 变得可见之前就启动了,然后它在我不希望的死亡活动期间变得可见。

有谁知道一种简单的方法来确保 Toast 在启动死亡活动之前完全执行?从我发现的情况来看,我似乎必须创建一个“Timer”对象,而实际上我想要的只是一个简单的 while 循环,如“while(Toast.isVisable) {}”来将执行绑定到几个秒。

I'm trying to explicitly launch an intent to a new Activity, but I would like some code in the current Activity to finish executing first. I've done a bit of research, and have a couple of ideas to accomplish this but am thinking "there's gotta be an easier way to do this". Here is the relevant block of code:

        cpuToast(dmg);
        if (player_.getStatus() == false)
        {
            playerWon_ = false;
            Intent intent = new Intent(Main.this, Death.class);
            startActivity(intent);
        }

dmg is an int. cpuToast simply makes a String to display the damage and then calls show(). getStatus() returns whether or not the player was killed. In the event that player was killed, I launch a new intent which will play an animation of the players death. Unfortunately the "Death" Activity is being launched before the Toast even becomes visible, and then it becomes visible during the Death Activity which I do not want.

Does anyone know a simple way to ensure that the Toast executes fully prior to launching the Death Activity? From what I've found it looks like I'll have to create a "Timer" object when really all I want is a simple while loop like "while(Toast.isVisable) {}" to tie up the execution for a couple of seconds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我ぃ本無心為│何有愛 2024-11-17 09:11:17

当您开始祝酒时,您还可以向当前活动发布一条延迟消息;该延迟消息的可运行对象可以启动新的意图。

At the time you begin your toast, you can also post a delayed message to your current activity; the runnable of that delayed message can start the new intent.

我的黑色迷你裙 2024-11-17 09:11:17

一定要吐司吗?

如果您愿意使用自定义对话框(删除标题、按钮等),那么这可能是您最好的选择。使用处理程序在给定时间后关闭对话框并启动新活动。

Does it have to be a toast?

If you are up for the idea of using a custom dialog (removed title, buttons etc.) that is probably your best bet. Use a handler to dismiss the dialog after given time and start the new activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文