如何取消吐司

发布于 2024-10-06 22:31:26 字数 126 浏览 5 评论 0原文

我开发了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

燃情 2024-10-13 22:31:26

Toast.makeText 返回一个 Toast 对象。对此对象调用 cancel() 来取消它。

Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.

如痴如狂 2024-10-13 22:31:26

您可以为 Toast 指定的最短持续时间是 Toast.LENGTH_SHORT,其值为 0,但实际上是 2000 毫秒长。如果您想要比这更短,请尝试以下操作:

    final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 1000); //specify delay here that is shorter than Toast.LENGTH_SHORT

The shortest duration you can specify for the toast is Toast.LENGTH_SHORT which has a value of 0 but is actually 2000 milliseconds long. If you want it shorter than that, then try this:

    final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 1000); //specify delay here that is shorter than Toast.LENGTH_SHORT
┊风居住的梦幻卍 2024-10-13 22:31:26

我认为没有必要创建自定义吐司。

仅创建 Toast 类的单个实例。我们只是使用 toast.setText("string") 设置 toast 的文本,并在 onDestroy() 中调用 toast.cancel() 方法> 方法。

工作代码片段如下:

package co.toast;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ShowToastActivity extends Activity {
    private Toast toast = null;
    Button btnShowToast;

    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // creates only one toast object..
        toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);

        btnShowToast = (Button) findViewById(R.id.btnShowToast);

        btnShowToast.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                // only set text to toast object..
                toast.setText("My toast!");
                toast.show();
            }
        });
    }

    @Override
    protected void onDestroy() 
    {
        toast.cancel();
        super.onDestroy();
    }

    @Override
    protected void onStop () {
        super.onStop();
        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 using toast.setText("string"), and call toast.cancel() method in onDestroy() method.

Working code snippet as follows:

package co.toast;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ShowToastActivity extends Activity {
    private Toast toast = null;
    Button btnShowToast;

    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // creates only one toast object..
        toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);

        btnShowToast = (Button) findViewById(R.id.btnShowToast);

        btnShowToast.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                // only set text to toast object..
                toast.setText("My toast!");
                toast.show();
            }
        });
    }

    @Override
    protected void onDestroy() 
    {
        toast.cancel();
        super.onDestroy();
    }

    @Override
    protected void onStop () {
        super.onStop();
        toast.cancel();
    }
}

Hope this helpful to you..

孤独患者 2024-10-13 22:31:26

首先,您必须创建 toast 对象

private Toast toast;

然后创建将消息显示为 Toast 的方法

private void showToast(String text) {
    if (toast != null) toast.cancel(); // cancel previous toast 
    toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    toast.show();
}

当您必须取消 toast 时调用下面的方法

if (toast != null) {
    toast.cancel();
}

当您必须显示 Toast 时调用下面的方法

showToast("Your Message");

First you have to create toast object

private Toast toast;

Then create method for Display message as Toast

private void showToast(String text) {
    if (toast != null) toast.cancel(); // cancel previous toast 
    toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    toast.show();
}

Now Call below method when you have to cancel toast

if (toast != null) {
    toast.cancel();
}

Call below method when you have to display Toast

showToast("Your Message");
つ低調成傷 2024-10-13 22:31:26

使用 tost 的取消方法:toast.cancel();

Use cancel method of tost : toast.cancel();

远山浅 2024-10-13 22:31:26

这是使用 Toastcancel() 方法的基本示例。

Toast mytoast;
mytoast = Toast.makeText(getApplicationContext(), "Hi Ho Jorgesys! ", Toast.LENGTH_LONG);
mytoast.show();
....
....
....
if(CancelToast){
  mytoast.cancel();
}

This is a basic example using the cancel() method of a Toast.

Toast mytoast;
mytoast = Toast.makeText(getApplicationContext(), "Hi Ho Jorgesys! ", Toast.LENGTH_LONG);
mytoast.show();
....
....
....
if(CancelToast){
  mytoast.cancel();
}
因为看清所以看轻 2024-10-13 22:31:26
Toast toast;

private void showToast(String text) {
    if (toast!=null)
        toast.cancel();
    toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    toast.show();
}
Toast toast;

private void showToast(String text) {
    if (toast!=null)
        toast.cancel();
    toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    toast.show();
}
原来是傀儡 2024-10-13 22:31:26

好的,我也在尝试取消 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() 中工作]

toast = Toast.makeText(ctxt, result, Toast.LENGTH_LONG);
    for (int i=0; i < 5; i++)
    {
        // Long-toasts each last about 3.5 secs
        toast.show();

        android.os.SystemClock.sleep(1500);
        toast.cancel();
        android.os.SystemClock.sleep(1500);
        toast = Toast.makeText(ctxt, result, Toast.LENGTH_LONG);
    }

好的,原始循环只包含执行显示的一行。
这本身就有效。

但为了实验,我添加了接下来的四行,以睡眠大约
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() ]

toast = Toast.makeText(ctxt, result, Toast.LENGTH_LONG);
    for (int i=0; i < 5; i++)
    {
        // Long-toasts each last about 3.5 secs
        toast.show();

        android.os.SystemClock.sleep(1500);
        toast.cancel();
        android.os.SystemClock.sleep(1500);
        toast = Toast.makeText(ctxt, result, Toast.LENGTH_LONG);
    }

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. ]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文