我想数 5 秒并启动一个功能。该函数还需要每 3 秒调用一次。
Toast.makeText(this, "Countdown 5", Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
for (int i = 5; i >= 0; --i) {
final int idx = i;
handler.postDelayed(() -> {
Toast.makeText(MyActivity.this, "Countdown " + (idx - 1), Toast.LENGTH_SHORT).show();
if (idx == 0) {
int messageSize = messageItems.size();
for (int j = 0; j < messageSize; j++) {
final int jdx = j;
Handler handler1 = new Handler();
handler1.postDelayed(() -> {
Toast.makeText(MyActivity.this, messageItems.get(jdx), Toast.LENGTH_SHORT).show();
}, 3000 * jdx);
}
}
}, 1000 * (5-idx));
}
内循环部分在没有外循环的情况下效果很好。
int messageSize = messageItems.size();
for (int j = 0; j < messageSize; j++) {
final int jdx = j;
Handler handler1 = new Handler();
handler1.postDelayed(() -> {
Toast.makeText(MyActivity.this, messageItems.get(jdx), Toast.LENGTH_SHORT).show();
}, 3000 * jdx);
}
我想添加一个功能,比如 5 秒倒计时。然后启动 Toast Message。而且 Toast 消息也需要每 3 秒敬酒一次。
慕田峪9158850
相关分类