Problem with display multiple Toast in order one after another

可紊 提交于 2020-05-08 07:22:07

问题


sorry for my bad English.
i want show two toast in order, in other word when first toast duration is over second toast appear.
this is my code :

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Toast2", Toast.LENGTH_SHORT).show();

but only second toast message will appear. i think when show method of second toast will execute it will cancel previous toast (first toast)

I solved my problem with this code :

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
    Handler handler =new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Toast.makeText(MainActivity.this, "Toast2", Toast.LENGTH_SHORT).show();
        }
    },1000);

but is there any easier solution?


回答1:


but only second toast message will appear. i think when show method of second toast will execute it will cancel previous toast (first toast)

When you call show method, it will put into message queue of UI thread, and the Toast will be shown in order. But you put two Toast at the same time, the latter will overlap the former.

i want show two toast in order, in other word when first toast duration is over second toast appear.

From Toast duration

private static final int LONG_DELAY = 3500; // 3.5 seconds 
private static final int SHORT_DELAY = 2000; // 2 seconds

To make the second toast display after duration of the first one, change your code to

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        Toast.makeText(MainActivity.this, "Toast2", Toast.LENGTH_SHORT).show();
    }
}, 2000);

but is there any easier solution?

Using Handler is the easy and simple solution to achieve your task.




回答2:


There are two ways to achieve it

  • Method 1: Use Thread as you used but use timer and execute one by one
  • Method 2: Use any Loop, For Example use For Loop



回答3:


Other solution is to use AlertDialog

createDialog().show();

with two methods createDialog() and continueDialog()

public AlertDialog createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Toast1")
            .setPositiveButton("Next",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            continueDialog().show();
                        }
                    });
    return builder.create();
}

public AlertDialog continueDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Toast2")
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
    return builder.create();
}



回答4:


This should help you :

Toast.makeText(this, "Show toast 1", Toast.LENGTH_SHORT).show();
    new Thread(){
        @Override
        public void run() {
            try{
                sleep(Toast.LENGTH_SHORT); // sleep the time first toast is being shown
                Toast.makeText(this, "Show toast 2", Toast.LENGTH_SHORT).show();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }.start();


来源:https://stackoverflow.com/questions/52333750/problem-with-display-multiple-toast-in-order-one-after-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!