如何立即更换一个第二电流吐司而不等待当前之一完成? [英] How to immediately replace the current toast with a second one without waiting for the current one to finish?

查看:175
本文介绍了如何立即更换一个第二电流吐司而不等待当前之一完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多按钮。和他们每个人的点击我告诉敬酒。不过,虽然敬酒负载和秀景,另一个按钮被点击并不会显示敬酒,直到正在显示完成的人。

I have many buttons. And on click of each of them I show a Toast. But while a toast loads and show in view, another button is clicked and the toast is not displayed until the one that is being displayed finishes.

所以,我想整理出一个方法来检测是否敬酒在目前的情况下显示。有没有办法知道是否正在显示,这样我可以取消它,并显示一个新的敬酒。

So, I would like to sort out a way to detect if a toast is showing in the current context. Is there a way to know if a toast is being displayed such that I can cancel it and display a new one.

推荐答案

您可以缓存在活动的可变电流吐司,然后只是显示下一个敬酒之前取消它。下面是一个例子:

You can cache current Toast in Activity's variable, and then cancel it just before showing next toast. Here is an example:

Toast m_currentToast;

void showToast(String text)
{
    if(m_currentToast != null)
    {
        m_currentToast.cancel();
    }
    m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
    m_currentToast.show();

}


另一种方式来即时更新吐司消息:

void showToast(String text)
{
    if(m_currentToast == null)
    {   
        m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
    }

    m_currentToast.setText(text);
    m_currentToast.setDuration(Toast.LENGTH_LONG);
    m_currentToast.show();
}

这篇关于如何立即更换一个第二电流吐司而不等待当前之一完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆