Problem with display multiple Toast in order one after another

前端 未结 4 506
心在旅途
心在旅途 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 03:56

    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();
    
    0 讨论(0)
  • 2020-12-18 04:10

    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
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 04:17

    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();
    }
    
    0 讨论(0)
提交回复
热议问题