如何prevent多吐司重叠 [英] How to prevent Multiple Toast Overlaps

查看:110
本文介绍了如何prevent多吐司重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个共同的myToast,我用myToast.cancel();之前发出新的敬酒为Android V2.3及以前,它的伟大工程,当一个新的敬酒需要发送。 ,旧的,如果还是屏幕上的,将被取消(并立即消失)被替换为新的敬酒。这避免堆积了一堆祝酒词,如果用户presses需要警报一键多次(和其他条件)。我的实际情况是,当一个错键是pressed,而另一个则出现清除键不是pressed 1敬酒出现。

I've been using a common "myToast" which I use "myToast.cancel(); prior to issuing a new toast. For Android v2.3 and older, this works great. When a new toast needs to be sent, the old one, if still on-screen, is canceled (and disappears immediately) to be replaced with the new toast. This avoids stacking up a bunch of toasts if the user presses a key multiple times that needs the alert (and other conditions). My actual case is one toast appears when a wrong key is pressed, and another appears if the Clear key is not pressed.

有关Android的4.0和4.1,先下一敬酒杀死当前和下一个土司发出myToast.cancel()。目前取消()API并表示它将取消当前和下一个敬酒(这似乎很愚蠢)。为什么取消要忍受敬酒?

For Android 4.0 and 4.1, issuing a myToast.cancel() before the next toast kills both the current and the next toast. The current cancel() API does indicate it cancels the current AND the next toast (which seems rather stupid). Why cancel a toast you want to put up?

这使得取消工作,始终如一地的Andr​​oid版本的任何想法(和它的作品在V2.3及以上的方式)?

Any ideas on making cancel work consistently across Android versions (and the way it works in v2.3 and older)?

我会尝试一些不雅的双面包系统跟踪其土司在使用,但似乎这样的痛苦工作围绕4.x中这种不良行为得到什么完美的作品,并在逻辑上老年人的Andr​​oid版本。

I'll try some inelegant dual toast system with tracking for which toast is in use, but it seems such a pain work around this bad behavior in 4.x to get what works perfectly and logically in older Android versions.

好吧,我解决了这个问题,但它几乎没有干净的,因为我所希望的。我实现了一个双敬酒的方式,它交替2祝酒之间。首先,我们的OnCreate中定义的祝酒词为活动前:

Ok, I solved it, but it's not nearly as clean as I would have liked. I implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:

Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;

在OnCreate:

In the OnCreate:

toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();

最后,当我需要显示的敬酒,并在同一时间取消了之前敬酒我使用类似于:

And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:

        if (lastToast0) {
            toast0.cancel();
            toast1.setDuration(Toast.LENGTH_LONG);
            toast1.setText("new message");
            toast1.show();
            lastToast0 = false;
        } else {
            toast1.cancel();
            toast0.setDuration(Toast.LENGTH_LONG);
            toast0.setText("new message");
            toast0.show();
            lastToast0 = true;
        }

如果您只是需要取消现有吐司(之前超时)使用方法:

If you need to just cancel an existing toast (before it times out) use:

            toast0.cancel();
            toast1.cancel();

通过Nexus 7(4.1),仿真器4.0,和一些设备采用Android 2.2,2.3。

Tested on Nexus 7 (4.1), Emulator 4.0, and several devices with Android 2.2, 2.3.

推荐答案

而不是调用取消。尝试重置文字和呼叫显示()。这应自行取消最后一个土司

Instead of calling cancel. try resetting the text and call show(). This should cancel the last toast by itself

myToast.setText("wrong key")
myToast.show();

如果您继续使用创建1每次同myToast相反,我想他们不会堆起来。

If you keep using the same myToast instead of creating one everytime i guess they wont stack up.

这篇关于如何prevent多吐司重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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