如何在一定时间内显示吐司

时间:2019-07-10 04:52:37

标签: android android-toast

可以在不使用Toast.LENGTH_LONG或Toast.LENGTH_SHORT的情况下在一定时间内设置Android Toast。

这可以通过非常简单的代码来实现:

private void customToast{
 long delayTimeForToast = 3500 //3.5sec

 Toast.makeText(getApplicationContext(),"Your TEXT", (int)toastDelayTime).show();
}

该Toast可见3.5秒

1 个答案:

答案 0 :(得分:0)

尝试从第show a Toast for a specific duration页获得此参考

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