gpt4 book ai didi

android - 依次显示多个 Toast 的问题

转载 作者:行者123 更新时间:2023-12-03 18:35:15 26 4
gpt4 key购买 nike

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

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

但只会出现第二条 toast 消息。我认为当执行第二个 toast 的 show 方法时,它将取消以前的 toast(第一个 toast)

我用这段代码解决了我的问题:
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 only second toast message will appear. i think when show method of second toast will execute it will cancel previous toast (first toast)



当您调用 show方法,它会被放入 UI 线程的消息队列中,并且 Toast 会按顺序显示。但是你同时放了两个Toast,后者会和前者重叠。

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



来自 Toast duration
private static final int LONG_DELAY = 3500; // 3.5 seconds 
private static final int SHORT_DELAY = 2000; // 2 seconds

要在第一个 toast 持续时间之后显示第二个 toast,请将您的代码更改为
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?



使用 Handler 是完成任务的简单易行的解决方案。

关于android - 依次显示多个 Toast 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com