【问题标题】:How i can show 3 Toasts with delay of 1 second for each我如何显示 3 个 Toasts,每个 Toasts 延迟 1 秒
【发布时间】:2015-07-28 10:53:30
【问题描述】:

我想显示Toast 3 次,每次延迟 1 秒,第三次祝酒后我的游戏重新开始。

这是我的代码,通过这个 toast 只显示一次,持续 3 秒,然后我的游戏重新开始。

    toast = new Toast(this);
    TextView textView=new TextView(this);
    textView.setTextColor(Color.CYAN);
    textView.setBackgroundColor(Color.TRANSPARENT);
    textView.setTextSize(40);
    textView.setText("Game will be start again in 3 seconds");
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

    toast.setView(textView);

// Here is the dialog when i press "NO" button then toast appear for 3 seconds just one's.


 alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        // dialog.cancel();
                        // GamePanel.thread.resume();
                        dialog.dismiss();
                        timer  = new CountDownTimer(3000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                toast.show();
                            }

                            @Override
                            public void onFinish() {
                                toast.cancel();

                                GamePanel.thread.setRunning(true);

                            }

                        }.start();

                        return;
                    }
                }

        );
        alertDialog.show();

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

【问题讨论】:

    标签: java android android-toast


    【解决方案1】:
        // show toast 1
    
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                // show toast 2.
            }
        }, 3000);
    
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                // show toast 3.
            }
        }, 6000);
    
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                //  Init game.
            }
        }, 7000);
    

    【讨论】:

    • 谢谢你能编辑我的代码并告诉我它是如何工作的
    • 非常感谢你能告诉我一件事吗。我的游戏是在最后一个吐司出现之前开始的,我该如何解决。??
    • 用一个postDelayed延迟游戏,只要设置为7000或更多。
    • 谢谢哥们我明白了
    • 天哪!那么,如果你需要倒数 10,你需要 11 个处理程序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2015-07-25
    相关资源
    最近更新 更多