16

In Android, when I create Toast and show them, they appear consecutively. The problem is that I have a button that checks some fields and if the user enters incorrect data, a Toast is shown. If the user touches the button repeatedly, Toasts are accumulated and the message does not disappear for a couple of seconds.

Which is the best way to avoid that?

  • May I save the reference to the last Toast and remove it before making a new one?
  • Should I use the same Toast for all messages?
  • Might I use any method that clears all the Application Toasts before making and showing the new one?
4

4 回答 4

36

您可以使用 的cancel()方法Toast关闭正在显示的 Toast。

使用变量来保持对每个 Toast 的引用,并cancel()在显示另一个 Toast 之前调用它。

private Toast mToast = null; // <-- keep this in your Activity or even in a custom Application class

//... show one Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();

//... show another Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();

// and so on.

你甚至可以把它包装成一个像这样的小类:

public class SingleToast {

    private static Toast mToast;

    public static void show(Context context, String text, int duration) {
        if (mToast != null) mToast.cancel();
        mToast = Toast.makeText(context, text, duration);
        mToast.show();
    }
}

并在您的代码中使用它,如下所示:

SingleToast.show(this, "Hello World", Toast.LENGTH_LONG);

//

于 2013-09-07T18:53:50.357 回答
1

在此活动中只吃一个 Toast。

private Toast toast = null;

然后Toast在创建另一个之前检查当前是否正在显示。

if (toast == null || !toast.getView().isShown()) {
    if (toast != null) {
        toast.cancel();
    }
    toast = Toast.makeToast("Your text", Toast.LENGTH).show();
}

showToast(text)如果您需要显示不同的文本消息,您甚至可以将最后一个片段变成一个私有方法来重构代码。

于 2013-09-07T19:15:40.547 回答
1

在 Kotlin 我使用这个:

private lateinit var toast: Toast

fun showToast(@StringRes stringId: Int, toastLength: Int = Toast.LENGTH_SHORT)
{
    if (this::toast.isInitialized)
    {
        toast.cancel()
    }

    toast = Toast.makeText(
        requireContext(),
        getString(stringId),
        toastLength
    )

    toast.show()
}

或者当在许多片段中使用它时,可以扩展Fragment类,因此函数showToast不必在每个片段中。

open class OneToastFragment : Fragment()
{
    private lateinit var toast: Toast

    fun showToast(@StringRes stringId: Int, toastLength: Int = Toast.LENGTH_SHORT)
    {
        if (this::toast.isInitialized)
        {
            toast.cancel()
        }

        toast = Toast.makeText(
            requireContext(),
            getString(stringId),
            toastLength
        )

        toast.show()
    }
}

此外,使用Toasty 库也很容易。

摇篮项目:

repositories {
    ...
    maven { url "https://jitpack.io" }
}

Gradle 模块应用程序:

dependencies {
    ...
    implementation 'com.github.GrenderG:Toasty:1.4.2'
}

Activity 类中的 onCreate:

Toasty.Config.getInstance().allowQueue(false).apply(); // set this to avoid toast acumulations

//Test:
int x = 0;
Toasty.info(this, Integer.toString(x++), Toast.LENGTH_SHORT, true).show();
Toasty.info(this, Integer.toString(x++), Toast.LENGTH_SHORT, true).show();
Toasty.info(this, Integer.toString(x++), Toast.LENGTH_SHORT, true).show();

//This will only show a toast with message `2` 
于 2020-07-28T10:38:32.533 回答
0

无论用户按下该按钮多少次,这只会在预定义的延迟(在这种情况下为 3 秒)之后制作新的 toast。

科特林

var mLastToastTime: Long = 0
val mNewToastInterval: Int = 3000 // milliseconds

if (System.currentTimeMillis() - mLastToastTime > mNewToastInterval) {
    showToast()
    mLastToastTime = System.currentTimeMillis().toInt()
}

爪哇

int mLastToastTime = 0;
int mNewToastInterval = 3000; // milliseconds

if (System.currentTimeMillis() - mLastToastTime > mNewToastInterval) {
       showToast();
       mLastToastTime = System.currentTimeMillis();
}
于 2021-02-08T07:47:36.173 回答