如何分别在两个for循环中使用postDelayed或sleep两次

我想数 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 秒敬酒一次。


萧十郎
浏览 76回答 1
1回答

慕田峪9158850

试试这个代码。这给出了相当准确的结果。有时会有一些滞后,但不会明显。Tweek 这个来展示你的祝酒词。&nbsp; &nbsp; int counter = 5;&nbsp; &nbsp; Handler handler = new Handler();&nbsp; &nbsp; ArrayList<String> messageItems = new ArrayList<>();&nbsp; &nbsp; messageItems.add("Message 1");&nbsp; &nbsp; messageItems.add("Message 2");&nbsp; &nbsp; messageItems.add("Message 3");&nbsp; &nbsp; Runnable runnable = new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counter > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("HOME", "onCreate: countdown " + counter + " " + System.currentTimeMillis());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler.postDelayed(this, 1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counter == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < messageItems.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int jdx = j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Handler handler1 = new Handler();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler1.postDelayed(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("HOME", "onCreate: countdown " + messageItems.get(jdx) + " " + System.currentTimeMillis());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 3000 * jdx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; Log.d("HOME", "onCreate: countdown 5 " + System.currentTimeMillis());&nbsp; &nbsp; handler.postDelayed(runnable, 1000);以下是计时的输出 -Countdown 5 1551720212370+1004Countdown 4 1551720213374+1019Countdown 3 1551720214393+1013Countdown 2 1551720215406+1568Countdown 1 1551720216974+413Message 1 1551720217387+2589Message 2 1551720219976+2999Message 3 1551720222975
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java