自定义 Toast 取消不起作用
我看到很多关于取消吐司的问题。他们都没有工作。
我有一个定制的吐司。其代码除了一行之外均与 http:// 相同developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
区别如下。
- toast变量被定义为类变量
- 整个java代码都写在一个方法中。
在此方法的开头,我添加了以下行来取消 toast。
if (toast!=null){ 吐司.取消(); }
当用户选择(onClick)视图/布局时调用该方法。问题是当用户选择几次时,toast 将排队(toast.cancel 不起作用)。
有什么解决办法吗?
[更新] 我尝试将 toast 对象设置为静态变量。还是不行。
I have seen a number of questions on cancelling toast. None of them is working.
I have a custom Toast. The code for that is all but one line same as http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
The difference is as follows.
- The toast variable is defined as a class variable
- The entire java code is written in a method.
In the start of this method, I have added the following line to cancel the toast.
if (toast!=null){ toast.cancel(); }
The method is called when user selects (onClick) the view/layout. The issue is when the user selects few times, the toast will get queued up (the toast.cancel is not working).
Any solutions?
[update]
I tried making toast object a static variable. Still dont work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最后,我创建了一个自定义对话框,以便阻止用户执行其他任何操作(并避免弹出多个 toast)。添加了 onClick 侦听器事件以在用户单击该事件时关闭对话框。
遗憾的是 Toast.cancel() 不起作用。
In the end, I created a Custom Dialog so that the user is blocked from doing anything else (and avoids multiple toasts popping up). Added a onClick Listener Event to close the dialog when user clicks the same.
Sad that Toast.cancel() doesn't work.
在上面的代码中
toast.setDuration(Toast.LENGTH_LONG);
因为你使用toast.setDuration(Toast.LENGTH_SHORT);
或设置特定时间。Toast 会自动取消。我们无法取消它
in above code
toast.setDuration(Toast.LENGTH_LONG);
in that u use
toast.setDuration(Toast.LENGTH_SHORT);
or se the particular time.Toast is cancelled automatically.we can't cancel it
使用此代码来自定义文本:
Use this code for custom text:
我遇到了同样的问题(自定义吐司排队)并找到了解决方案。在我的例子中效果很好。
将自定义 toast 对象
初始设置为 null
。如果为空,则使用“new”创建新的自定义 toast 对象。
只要您处于相同的活动中,就不要“新建”来创建新对象。相反,使用该对象。由于
setText()
在这种情况下不起作用,请像处理自定义 Toast 一样使用setView()
。通过这种方式,
show()、cancel()、show()、cancel()
完全按照我的预期工作。没有延迟,没有排队。希望这有帮助。
I suffered from same issue (custom toast queuing up) and found a solution. It worked fine in my case.
Having custom toast object
initially set to null
.If this is null, create new custom toast object with "new".
As far as you are in same activity, don't "new" to create new object. Instead, use that object. Since
setText()
won't work in this case, usesetView()
as you do with your custom toast.With this way
show(), cancel(), show(), cancel()
worked exactly as I expect. No delay, no queuing.Hope this helps.