我怎么能显示3个延迟1秒的Toast

时间:2015-07-28 10:53:30

标签: java android android-toast

我想展示Toast 3次,每次延迟1秒,第3次吐司后我的游戏重新开始。

这是我的代码通过这个吐司只显示一次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);
}

1 个答案:

答案 0 :(得分: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);