【问题标题】:Best way to avoid Toast accumulation in Android在 Android 中避免 Toast 堆积的最佳方法
【发布时间】:2013-09-07 18:23:00
【问题描述】:

在 Android 中,当我创建 Toast 并显示它们时,它们会连续出现。问题是我有一个检查某些字段的按钮,如果用户输入了不正确的数据,则会显示一个 Toast。如果用户反复触摸按钮,则 Toast 会累积,并且消息不会在几秒钟内消失。

避免这种情况的最佳方法是什么?

  • 我可以保存对最后一个 Toast 的引用并在创建新 Toast 之前将其删除吗?
  • 我应该对所有消息使用相同的 Toast 吗?
  • 我可以使用任何方法在制作和展示新的 Toast 之前清除所有 Application Toast 吗?

【问题讨论】:

  • 我也有同样的问题,我与您分享一些没有用的想法,使 toast 最终化,也使用 if 条件,使用计数器.. :\ 我现在会看这个问题。
  • Hola(Hello) Didac,你试过thisthat 吗?
  • 最好的方法是不使用 Toasts。 :) 可能

标签: java android


【解决方案1】:

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

使用一个变量来保持对您显示的每个 Toast 的引用,并在显示另一个 Toast 之前调用 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);

//

【讨论】:

  • SingleToast 解决方案有效,但存在内存问题。它在mToast 的生命周期内保留context,这可能会超过context 的潜在生命周期。应该使用context.getApplicationContext() 来避免内存泄漏。
【解决方案2】:

在此活动中只有一个 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();
}

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

【讨论】:

    【解决方案3】:

    在 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 library 也很容易。

    Gradle 项目:

    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` 
    

    【讨论】:

      【解决方案4】:

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

      科特林

      var mLastToastTime: Long = 0
      val mNewToastInterval: Int = 3000 // milliseconds
      
      if (System.currentTimeMillis() - mLastToastTime > mNewToastInterval) {
          showToast()
          mLastToastTime = System.currentTimeMillis().toInt()
      }
      

      Java

      int mLastToastTime = 0;
      int mNewToastInterval = 3000; // milliseconds
      
      if (System.currentTimeMillis() - mLastToastTime > mNewToastInterval) {
             showToast();
             mLastToastTime = System.currentTimeMillis();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 1970-01-01
        • 2018-02-11
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多