依次显示多个Toast的问题

依次显示多个Toast的问题

本文介绍了依次显示多个Toast的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语不好.
我要按顺序显示两个烤面包,换句话说,当第一个烤面包持续时间超过第二个烤面包时.
这是我的代码:

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();

,但仅会显示第二个吐司消息.我认为第二个Toast的show方法将执行时,它将取消之前的Toast(第一个Toast)

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?

推荐答案

调用 show 方法时,它将放入UI线程的消息队列中,并且Toast将按顺序显示.但是您同时放了两个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.

来自敬酒时间

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);

使用处理程序是完成任务的简单方法.

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

这篇关于依次显示多个Toast的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 23:21