gpt4 book ai didi

java - 造成程序执行延迟

转载 作者:行者123 更新时间:2023-12-01 18:05:10 26 4
gpt4 key购买 nike

我想在我的代码中创建一个延迟,其中一部分应在 5 秒后执行,另一部分应在 10 秒后执行。我已经尝试过 TRY CAtcH 线程,但不知道问题出在哪里..这是我的代码,请看一下..先谢谢了....

 // This portion to be executed after 5 seconds 

val1 = Integer.parseInt(String.valueOf(unit.getText()));
val2 = Integer.parseInt(String.valueOf(unit2.getText()));
result = val1 + val2;

val = Integer.toString(result);
int len = val.length();

if (len == 1) {

num1 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 1)));

unitanswer1.setText(Integer.toString(num1));
unitanswer2.setText(Integer.toString(0));
unitanswer2.setVisibility(View.INVISIBLE);

} else {
num1 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 1)));
unitanswer1.setText(Integer.toString(num1));

num2 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 2)));
unitanswer2.setText(Integer.toString(num2));
}

//这部分10秒后执行

             val1 = Integer.parseInt(String.valueOf(tens.getText()));
val2 = Integer.parseInt(String.valueOf(ten2.getText()));
val3=Integer.parseInt(String.valueOf(unitanswer2.getText()));

result = val1 + val2 + val3;

val = Integer.toString(result);
len = val.length();

if (len == 1) {

num1 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 1)));

tenanswer1.setText(Integer.toString(num1));
tenanswer2.setText(Integer.toString(0));
tenanswer2.setVisibility(View.INVISIBLE);

}
else {
num1 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 1)));
tenanswer1.setText(Integer.toString(num1));

num2 = Integer.parseInt(String.valueOf(val.toString().charAt(len - 2)));
tenanswer2.setText(Integer.toString(num2));

//Animation's work to be done here.....
//remaining possibilites of addition....
}
}

最佳答案

试试这个:

    new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This portion to be executed after 5 seconds
...
}
},5*1000);

还有这个:

    new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//This portion to be executed after 10 seconds
...
}
},10*1000);

希望对您有帮助。

关于java - 造成程序执行延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37049954/

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