【问题标题】:Showing a Toast Message for 30 seconds显示 Toast 消息 30 秒
【发布时间】:2019-12-30 12:42:29
【问题描述】:

有没有办法让 Toast 消息显示 30 秒(没有闪烁效果)?

我已经尝试过这篇文章中的方法,但是它们都不适合我(Android 9)

Can an Android Toast be longer than Toast.LENGTH_LONG?

最重要的是,我无法针对我的情况使用 Snackbar 或状态栏通知功能

【问题讨论】:

标签: android android-toast


【解决方案1】:

你来了!

private Toast mToastToShow;
    public void showToast(View view) {
       // Set the toast and duration
       int toastDurationInMilliSeconds = 10000;
       mToastToShow = Toast.makeText(this, "Hello world, I am a toast.",                                                    Toast.LENGTH_LONG);

       // Set the countdown to display the toast
       CountDownTimer toastCountDown;
       toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
          public void onTick(long millisUntilFinished) {
             mToastToShow.show();
          }
          public void onFinish() {
             mToastToShow.cancel();
             }
       };

       // Show the toast and starts the countdown
       mToastToShow.show();
       toastCountDown.start();
    }

【讨论】:

  • 已经测试过那个并且没有用(至少对我来说在 Android 9 上)这对你有用吗?如果有,在哪个 Android 版本上?
  • 我目前在 Android 10 上运行,并且运行良好。尝试像这里一样使用 Snackbar mobikul.com/how-to-make-customizable-snack-bar-in-android
  • toast 是闪烁还是持续 10 秒?
  • 它是持久化的。
【解决方案2】:

简答:

详细回答:

您不能根据official documentations 制作具有自定义持续时间的 Toast

持续时间:显示消息的时间。 LENGTH_SHORT 或 LENGTH_LONG 值是 LENGTH_SHORT 或 LENGTH_LONG

还有其他选择吗?

在您的情况下,您可以使用警告对话框、弹出窗口或第三方库,例如 KoushikToast

请注意,如果您想使用 KoushikToast 或任何其他库,您需要在清单中添加 SYSTEM_ALERT_WINDOW 权限并手动授予它或在 API 23 及更高版本上授予 programmatically

【讨论】:

    【解决方案3】:

    您可以使用SuperToast 向用户显示吐司

    您可以通过其他方式显示一个不可取消的对话框并在 30 秒内将其放入线程中将其关闭,但您必须处理视图和上下文,这是一项非常艰巨的工作

    你可以试试这个代码也许对你有用:

    private Toast mToastToShow;
    public void showToast(View view) {
       // Set the toast and duration
       int toastDurationInMilliSeconds = 10000;
       mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", 
                                                    Toast.LENGTH_LONG);
    
       // Set the countdown to display the toast
       CountDownTimer toastCountDown;
       toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 
                                       1000 /*Tick duration*/) {
          public void onTick(long millisUntilFinished) {
             mToastToShow.show();
          }
          public void onFinish() {
             mToastToShow.cancel();
             }
       };
    
       // Show the toast and starts the countdown
       mToastToShow.show();
       toastCountDown.start();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 2011-12-09
      • 2014-07-04
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      相关资源
      最近更新 更多