避免安卓系统中Toast累积的最佳方法[英] Best way to avoid Toast accumulation in Android

本文是小编为大家收集整理的关于避免安卓系统中Toast累积的最佳方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在Android中,当我创建吐司并向他们展示时,他们会连续出现.问题是我有一个按钮检查某些字段,如果用户进入错误数据,则显示吐司.如果用户反复触摸按钮,则累积吐司,并且消息不会消失几秒钟.

这是避免这种情况的最佳方式吗?

  • 我可以保存对最后一个吐司的引用,并在制作新的吐司之前删除它?
  • 我应该为所有消息使用相同的吐司吗?
  • 我可能会使用任何清除所有应用程序的方法在制作和显示新的应用程序之前?

推荐答案

您可以使用Toast的cancel()方法关闭显示吐司.

在显示它时,使用变量来关注每个吐司,并只需调用cancel()之前显示另一个.

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);

//

其他推荐答案

在此活动中只有一个吐司.

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)以重构代码.

其他推荐答案

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()
    }
}

此外,它可以很容易地使用烤肉库.

Gradle项目:

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

gradle模块应用程序:

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

活动类中的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` 

本文地址:https://www.itbaoku.cn/post/102557.html

问题描述

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?

推荐答案

You can use the cancel() method of Toast to close a showing Toast.

Use a variable to keep a reference to every Toast as you show it, and simply call cancel() before showing another one.

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.

You could even wrap that into a small class like so:

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();
    }
}

and use it in your code like so:

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

//

其他推荐答案

Have only one Toast in this activity.

private Toast toast = null;

Then just check if there's currently a Toast being shown before creating another one.

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

You can even make that last snippet into a private method showToast(text) to refactor code if you need to display different text messages.

其他推荐答案

In Kotlin I use this:

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()
}

Or when using it in many fragments it is possible to extend the Fragment class, so function showToast doesn't have to be in every fragment.

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()
    }
}

Also, it can be easy using Toasty library.

Gradle project:

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

Gradle module app:

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

onCreate in Activity class:

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`