Problem with display multiple Toast in order one after another

前端 未结 4 517
心在旅途
心在旅途 2020-12-18 03:22

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 :

Toas         


        
4条回答
  •  不思量自难忘°
    2020-12-18 04:13

    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.

提交回复
热议问题