如何取消吐司
我开发了一个 Android 应用程序,但我遇到了 Toast
的问题。 假设我正在显示一个Toast,它显示在应用程序窗口上。 当出现对话框时,toast 不会立即消失。
我想知道如何取消吐司。
I developed an android application and I am facing a problem with Toast
.
Suppose I am displaying a Toast, it is displayed on the application window.
When a Dialog box is appears, the toast doesn't disappear instantly .
I want to know how I can cancel the toast.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Toast.makeText
返回一个Toast
对象。对此对象调用cancel()
来取消它。Toast.makeText
returns aToast
object. Callcancel()
on this object to cancel it.您可以为 Toast 指定的最短持续时间是
Toast.LENGTH_SHORT
,其值为0
,但实际上是2000 毫秒长
。如果您想要比这更短,请尝试以下操作:The shortest duration you can specify for the toast is
Toast.LENGTH_SHORT
which has a value of0
but is actually2000 milliseconds long
. If you want it shorter than that, then try this:我认为没有必要创建自定义吐司。
仅创建
Toast
类的单个实例。我们只是使用 toast.setText("string") 设置 toast 的文本,并在 onDestroy() 中调用toast.cancel()
方法> 方法。工作代码片段如下:
希望这对您有帮助..
I think there is no need to create a custom toast.
Create only single instance of the
Toast
class. We just set the text of the toast usingtoast.setText("string")
, and calltoast.cancel()
method inonDestroy()
method.Working code snippet as follows:
Hope this helpful to you..
首先,您必须创建 toast 对象
然后创建将消息显示为 Toast 的方法
当您必须取消 toast 时调用下面的方法
当您必须显示 Toast 时调用下面的方法
First you have to create toast object
Then create method for Display message as Toast
Now Call below method when you have to cancel toast
Call below method when you have to display Toast
使用 tost 的取消方法:toast.cancel();
Use cancel method of tost : toast.cancel();
这是使用
Toast
的cancel()
方法的基本示例。This is a basic example using the
cancel()
method of aToast
.好的,我也在尝试取消 Toast,但似乎无法取消
在 Activity.onDestroy() 中调用 cancel() 调用
或 Activity.onStop() 等。经过一些 API 文档阅读和
谷歌搜索其他需要帮助取消祝酒词的人,
我想我仍然不清楚活动何时停止,
暂停,被摧毁。我需要一种可靠的方式来强制我的活动
暂停或停止。
在我的具体情况下,因为只有短和长
持续时间 Toasts,我决定迭代循环 5 次
长时间 Toast 的 show(),因此它会保留在屏幕上
15-20 秒。效果很好!
但是,使用 Toast 对象的缺点(负面影响)
是即使在用户放弃您的应用程序之后它们仍然存在并且
返回主屏幕并开始使用其他应用程序...您的
Toast 会在接下来的 15-20 秒内存活,除非你
可以保证你能找到某个地方(某种方式)
调用取消()。另外,你必须相信Android会
尊重您对 cancel() 的调用!
因此,为此,我一直在调整我的简单循环,试图
invoke 在循环中取消,并向自己证明它会
尊重取消电话,并在视觉上表现如预期。
代码片段:
注意:“toast”是一个公共实例变量,所以我们
只有一个 Toast 对象实例 [ 按原样
上面推荐的,并且评论者证实了这一点
两年前在 Activity onStop() 和 OnDestroy() 中工作]
好的,原始循环只包含执行显示的一行。
这本身就有效。
但为了实验,我添加了接下来的四行,以睡眠大约
3.5 秒显示到一半,然后取消,再次睡眠
一分半钟,然后重新创建并再次显示 Toast。
我预计会看到吐司大约 1.5 秒,然后看到它消失,
然后再过 1.5 秒,等等。
猜猜看……吐司根本就没有出现!
好吧,我完全处于咕哝模式……我错过了什么,在
了解 Toast 类的内在奥秘
已实施并且应该表现?
并且,回到我的第一个问题:如何最好地获得我的
活动进入暂停或停止状态?
[注:我读了很多这个论坛......太棒了!这是我的第一篇文章
进入主题讨论的中间...抱歉我的回复
被标记为答案,而不是相关的问题
到这个线程的主题。 ]
Ok, I too am trying to cancel a Toast, and can't seem to get
the cancel() call to get invoked in either the Activity.onDestroy()
or the Activity.onStop(), etc. After some API-doc reading and
googling of others needing help getting Toasts to get cancelled,
I'm thinking I'm still not clear on when Activities get stopped,
paused, destroyed. I need a sure-fire way to force my activity
to get paused or stopped.
In my specific case, since there are only short and long
duration Toasts, I decided to iterate a loop 5 times doing
a show() of a long-duration toast, so it would stay on-screen
for 15-20 seconds. That works fine!
But, the drawback (negative side-effect) of using a Toast object
is that they persist even AFTER the user abandons your app and
goes back to home-screen and starts using some other app...your
toast is gonna live for the next 15-20 seconds, unless you
can guarantee that you can find some place (some way) to
invoke cancel(). Also, you must believe that Android will
honor your call to cancel() !
So, to that end, I've been tweaking my simple loop, trying to
invoke cancels right in the loop, and prove to myself it will
honor a cancel call, and visually behave as expected.
Code snippet:
Note: 'toast' is a public INSTANCE variable, so we
have only ONE instance of the Toast-object [ as is
recommended above, and which a commenter confirmed
was working two years ago, in Activity onStop() and OnDestroy() ]
Ok, the original loop contained just that one line doing the show.
That works, by itself.
But to experiment, I added those next four lines, to sleep for about
half way thru the 3.5 second showing, then cancel it, sleep again
for a second and a half, and then re-create and show the Toast again.
I expected to see the toast for approx 1.5 secs, then see it disappear,
and come back on in another 1.5 secs, etc.
Guess what...the toast never appears AT ALL!
Ok, I'm in total mumble-mode...what am I missing, in
understanding the inner-mysteries of how the Toast-class
is implemented and is supposed to behave?
And, back to my first issue: How best to get my
Activity to go into a paused or stopped state?
[ Note: I READ this forum a LOT...it's great !!! This is my first posting
into the middle of a thread-discussion...Sorry that my reply
is getting flagged as an ANSWER, rather than as a QUESTION relating
to this thread's topic. ]