Android - 隐藏所有显示的 Toast 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2755277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:14:06  来源:igfitidea点击:

Android - Hide all shown Toast Messages

androidhidemessagestoast

提问by crazyV

How do I remove all toast messages currently displayed?

如何删除当前显示的所有 Toast 消息?

In my App, there is a list, when a user clicks on an item, a toast message is displayed, 10 items - 10 toast messages.

在我的应用程序中,有一个列表,当用户单击某个项目时,会显示一条吐司消息,10 项 - 10 条吐司消息。

So if the user clicks 10 times, then presses the menu button, they have to wait for some seconds until they're able to read the menu option text.

因此,如果用户点击 10 次,然后按下菜单按钮,他们必须等待几秒钟,直到他们能够阅读菜单选项文本。

It shouldn't be like that :)

不应该是这样的:)

回答by Mudar

My solution was to initialize a single Toast in the activity. Then changing its text on each click.

我的解决方案是在活动中初始化一个 Toast。然后在每次点击时更改其文本。

Toast mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
if (a) {
  mToast.setText("This is a");
  mToast.show();
} else if (b) {
  mToast.setText("This is b");
  mToast.show();
}

回答by CommonsWare

how do I disable all toast messages being process currently?

如何禁用当前正在处理的所有 Toast 消息?

You can cancel individual Toastsby calling cancel()on the Toastobject. AFAIK, there is no way for you to cancel all outstanding Toasts, though.

您可以取消单独Toasts调用cancel()Toast对象。AFAIK,但是您无法取消所有未完成的Toasts

回答by Justin Muller

What about checking if a toast is already being displayed?

检查吐司是否已经被显示呢?

private Toast toast;
...
void showToast() {
   if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
      toast = Toast.makeText(getActivity(), "Toast!", Toast.LENGTH_LONG);
      toast.show();
   }
}

回答by BasicPleasureModel

Mudar's solution worked beautifully for me on a similar problem - I had various toasts stacking up in a backlog after multiple buttonclicks.

在类似的问题上,Mudar 的解决方案对我来说效果很好——多次button点击后,我有各种吐司堆积在积压中。

One instance of Toast with different setText()sand show()s was the exactly the answer I was looking for - previous message cancelled as soon as a new button is clicked. Spot on

一个带有不同setText()sshow()s的 Toast 实例正是我正在寻找的答案 - 一旦单击新按钮,上一条消息就会取消。发现

Just for reference, here's what I did...

仅供参考,这就是我所做的......

In OnCreate:

OnCreate

    final Toast myToast = Toast.makeText(getBaseContext(), "", Toast.LENGTH_SHORT);

Within in each OnClick:

在每个内OnClick

myToast.setText(R.string.toast1);
myToast.show();

回答by user23

My solution is to save all toast references in a list and make a method to cancel all them when need it:

我的解决方案是将所有 toast 引用保存在一个列表中,并在需要时创建一个方法来取消所有引用:

private ArrayList<Toast> msjsToast = new ArrayList<Toast>();

private void killAllToast(){
    for(Toast t:msjsToast){
        if(t!=null) {
            t.cancel();
        }
    }
    msjsToast.clear();
}

When you create a Toast do this way and save the reference:

当您创建 Toast 时,请这样做并保存参考:

Toast t = Toast.makeText(context, "Download error: xx", Toast.LENGTH_LONG);
t.show();
msjsToast.addToast(t);

When you need to delete them:

当您需要删除它们时:

killAllToast();

You can create this like a static method in a global class and use it to kill all the toast of the app.

您可以像在全局类中创建静态方法一样创建它,并使用它来终止应用程序的所有 toast。

回答by Roymunson

Here is my simple answer to the problem:

这是我对这个问题的简单回答:

First in your activity create a global Toastobject.

首先在您的活动中创建一个全局Toast对象。

    private Toast example;

Now whenever you want to call a new Toast message just do this:

现在,每当您想调用新的 Toast 消息时,只需执行以下操作:

if(buttonClicked) {
    example.cancel();
    example = Toast.makeText(this, "" , Toast.LENGTH_SHORT);
    example.setText("Button Clicked");
    example.show();
}

This keeps all the Toasts in one central Toast and removes Toast spam. This is a quick rough solution so maybe there is a more elegant way to do it.

这将所有 Toast 保留在一个中央 Toast 中并删除 Toast 垃圾邮件。这是一个快速粗略的解决方案,所以也许有一种更优雅的方法来做到这一点。

回答by S.A.Jay

I think I found a way to make toasts message not queue up for me. Thought I would share.

我想我找到了一种方法来让 toasts 消息不为我排队。以为我会分享。

this part goes at top.

这部分位于顶部。

private Toast msg;    

This part goes in my setOnTouchListener()

这部分在我的 setOnTouchListener() 中

if(null == msg)
{
msg = Toast.makeText("Message to user!", Toast.LENGTH_SHORT);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();

//handels the stupid queueing toast messages
new Handler().postDelayed(new Runnable()
{
      public void run()
      {
          msg = null;

      }
}, 2000);

}

It is more of a hack than anything. But I show a toast message any time someone favorites a part of my app. And if they go crazy clicking the favorite button it will go crazy with the toasts messages. But not anymore. It will wait 2 seconds and then set my toast object to null and allow it to display again.

它更像是一种黑客攻击。但是每当有人喜欢我的应用程序的一部分时,我都会显示一条吐司消息。如果他们疯狂地点击最喜欢的按钮,那么敬酒消息就会变得疯狂。但现在不是了。它将等待 2 秒钟,然后将我的 toast 对象设置为 null 并允许它再次显示。

回答by Akash Gupta

Create a Toast object outside onClickfunction and use the code below. It will stop any existing Toast and start the latest Toast.

onClick函数外创建一个 Toast 对象并使用下面的代码。它将停止任何现有的 Toast 并启动最新的 Toast。

Toast mToast;

public void onClick(String abc) {

    if(mToast!=null)
        mToast.cancel();
    Context context = this;
    mToast = Toast.makeText(context, abc, Toast.LENGTH_SHORT);
    mToast.show();
}

回答by olearyj234

In Kotlin, it's an easy fix.

Kotlin 中,这是一个简单的修复。

In my example, i toggle the sort type when the user clicks on a button. If the user rapidly clicks on the button, it will cancel the currently displayed sortToastbefore another is shown.

在我的示例中,我在用户单击按钮时切换排序类型。如果用户快速点击按钮,它会在显示sortToast另一个之前取消当前显示。

private var sortToast: Toast? = null

sortButton.onClickListener {
    sortToast?.cancel()
    sortToast = Toast.makeText(context, "Sort by toggled", Toast.LENGTH_SHORT)
    sortToast?.show()
}

回答by Victor Odiah

This is how I do it.

我就是这样做的。

Toast toast;   

if(toast==null)
        toast=Toast.makeText(getApplicationContext(),R.string.act_now_private_post_text,Toast.LENGTH_LONG);
        else
            toast.setText(R.string.act_now_private_post_text);
        toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL,10,10);
        toast.show();