如何避免敬酒如果有一个已经被显示吐司 [英] How to avoid a Toast if there's one Toast already being shown

查看:119
本文介绍了如何避免敬酒如果有一个已经被显示吐司的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个搜索栏 onSeekBarProgressStop(),我要表现出吐司消息。

I have several SeekBar and onSeekBarProgressStop(), I want to show a Toast message.

但是,如果对搜索栏我执行迅速的行动,那么UI线程不知何故块和吐司消息等待,直到UI线程是免费的。

But if on SeekBar I perform the action rapidly then UI thread somehow blocks and Toast message waits till UI thread is free.

现在我的问题是要避免新的吐司消息,如果吐司消息已显示。或者是他们的任何条件,使我们检查UI线程目前是免费的话,我将展示吐司消息。

Now my concern is to avoid the new Toast message if the Toast message is already displaying. Or is their any condition by which we check that UI thread is currently free then I'll show the Toast message.

我尝试了两种方式,使用 runOnUIThread(),并创造新的处理程序

I tried it in both way, by using runOnUIThread() and also creating new Handler.

推荐答案

我已经尝试了各种各样的事来做到这一点。起初,我尝试使用取消(),其中有没有影响,我(另见的这个答案)。

I've tried a variety of things to do this. At first I tried using the cancel(), which had no effect for me (see also this answer).

使用 setDuration(N)我不来任何地方任。原来,通过登录 getDuration(),它带有一个0值(如 makeText()的参数是 Toast.LENGTH_SHORT )或1(如果 makeText()的参数是 Toast.LENGTH_LONG )。

With setDuration(n) I wasn't coming to anywhere either. It turned out by logging getDuration() that it carries a value of 0 (if makeText()'s parameter was Toast.LENGTH_SHORT) or 1 (if makeText()'s parameter was Toast.LENGTH_LONG).

最后,我想检查,如果敬酒的看法 isShown()。当然这不是如果没有吐司示出,但即使多,它在这种情况下返回一个致命的错误。所以,我需要尝试和捕获错误。 现在,如果显示举杯 isShown()返回true。 利用 isShown()我想出了一个方法:

Finally I tried to check if the toast's view isShown(). Of course it isn't if no toast is shown, but even more, it returns a fatal error in this case. So I needed to try and catch the error. Now, isShown() returns true if a toast is displayed. Utilizing isShown() I came up with the method:

    /**
     * <strong>public void showAToast (String st)</strong></br>
     * this little method displays a toast on the screen.</br>
     * it checks if a toast is currently visible</br>
     * if so </br>
     * ... it "sets" the new text</br>
     * else</br>
     * ... it "makes" the new text</br>
     * and "shows" either or  
     * @param st the string to be toasted
     */

    public void showAToast (String st){ //"Toast toast" is declared in the class
        try{ toast.getView().isShown();     // true if visible
            toast.setText(st);
        } catch (Exception e) {         // invisible if exception
            toast = Toast.makeText(theContext, st, toastDuration);
            }
        toast.show();  //finally display it
    }

这篇关于如何避免敬酒如果有一个已经被显示吐司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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