我想在我的代碼中創建一個延遲,使得一部分應該在5秒後執行,另一部分在10秒後執行。我試圖嘗試捕捉線程,但不知道哪裏是問題。這裏是我的代碼好心看看吧..在adavnce感謝....創建延遲執行程序
// 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....
}
}
使用兩['Timer'](https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html)秒。 –
好心編輯我的代碼 –
http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android – Manish