gpt4 book ai didi

java - 如何延迟多个 if 条件语句?

转载 作者:行者123 更新时间:2023-12-01 16:15:29 25 4
gpt4 key购买 nike

有没有办法可以在运行另一个 if 条件之前将 if 语句延迟 1 秒?我尝试为每个语句使用可运行的处理程序,但它使应用程序崩溃,我还使用了 Thread.sleep(1000);但它只是卡住了用户界面。我该如何解决这个问题?

这就是我第一次使用处理程序的方式

final Handler Internet_handler = new Handler();
final Runnable internet = new Runnable() {
public void run() {if (EseCards_Slot1.isShown()) {
EseCards_Slot1.performClick();
bot_count++;
Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
botThinkdelay();// delays the if statement
}
}
};
Internet_handler.postDelayed(internet, 1700);
if (EseCards_Slot1.isShown()) {
EseCards_Slot1.performClick();
bot_count++;
Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
botThinkdelay();// delays the if statement
}
if (EseCards_Slot2.isShown()) {
EseCards_Slot2.performClick();
bot_count++;
Stop_Two_InstantPLay();
botThinkdelay();
}
if (EseCards_Slot3.isShown()) {
EseCards_Slot3.performClick();
bot_count++;
Stop_Two_InstantPLay();
botThinkdelay();
}
if (EseCards_Slot4.isShown()) {
EseCards_Slot4.performClick();
bot_count++;
Stop_Two_InstantPLay();
botThinkdelay();
}
if (EseCards_Slot5.isShown()) {
EseCards_Slot5.performClick();
bot_count++;
Stop_Two_InstantPLay();
botThinkdelay();
}
if ((bot_count>0)&&(Ese_Turn_toPlay==true)) {
Market.performClick();
bot_count=0;
botThinkdelay();
}

最佳答案

我认为,您可以使用 Handler (请参阅 How to set delay in android?How to use delay functions in Android Studio? )。代码变得丑陋,但我只知道 Java 中 UI 线程的几个解决方案(CountDownTimer)。

import android.os.Handler;

final Handler handler = new Handler();

if (EseCards_Slot1.isShown()) {
EseCards_Slot1.performClick();
bot_count++;
Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (EseCards_Slot2.isShown()) {
EseCards_Slot2.performClick();
bot_count++;
Stop_Two_InstantPLay();
// Here comes the next Handler.
// It would be better to divide if statements into methods
// and run them inside each new handler.postDelayed.
}
}
}, 1000);

}

关闭屏幕后,您应该删除所有处理程序。

关于java - 如何延迟多个 if 条件语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62403167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com