显示吐司消息30秒

时间:2019-12-30 12:42:29

标签: android android-toast

有没有一种方法可以显示Toast消息30秒(没有闪烁效果)?

我已经尝试过本文中的方法,但是这些方法都不适合我(Android 9)

Can an Android Toast be longer than Toast.LENGTH_LONG?

最重要的是,我无法使用Snackbar或Statusbar Notification功能

3 个答案:

答案 0 :(得分:0)

您在这里!

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

答案 1 :(得分:0)

简短答案:

详细答案:

您不能根据official documentations使用自定义持续时间制作吐司

  

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

还有其他选择吗?

您可以使用警报对话框,弹出窗口或第三方库,例如KoushikToast

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

答案 2 :(得分:-2)

您可以使用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();
}