【问题标题】:How can I show a toast for a specific duration?如何在特定时间段内显示敬酒?
【发布时间】:2013-01-08 08:23:36
【问题描述】:

这是我必须显示Toast 500 毫秒的方式。不过,它显示的时间超过一秒钟。

Toast.makeText(LiveChat.this, "Typing", 500).show(); 

我怎样才能只显示Toast 500 毫秒?

【问题讨论】:

标签: android toast android-toast


【解决方案1】:

我不相信这可以做到,你只能使用Toast.LENGTH_LONGToast.LENTH_SHORT 你无法定义你知道的速度。

【讨论】:

    【解决方案2】:
    Toast.makeText(LiveChar.this,"Typing",Toast.LENGTH_SHORT);
    

    这是你唯一能做的..

    【讨论】:

      【解决方案3】:

      这是无法做到的。要显示长度小于Toast.LENGTH_SHORT 的吐司,您必须在所需时间后取消它。比如:

      final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
          toast.show();
      
          Handler handler = new Handler();
              handler.postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     toast.cancel(); 
                 }
          }, 500);
      

      【讨论】:

      • @Muraliganesan 如果它解决了您的问题,请接受答案
      • 另一个android api设计缺陷
      【解决方案4】:

      标准 Toast 无法满足您的要求。也许您应该考虑集成一个第三方库,它可以为您提供更好的 Toast 选项(名为 Crouton)。我自己没有用过,但人们似乎喜欢它。

      您无法在标准操作系统中控制 Toast 的长度。

      Crouton 链接:https://github.com/keyboardsurfer/Crouton

      【讨论】:

        【解决方案5】:

        这是做不到的。 Toast.LENGTH_SHORTToast.LENGTH_LONG 的值是 0 和 1。这意味着它们被视为标志而不是实际持续时间,所以我认为除了这些值之外,不能将持续时间设置为任何值。

        【讨论】:

          【解决方案6】:

          先试试。这会将 toast 设置为以毫秒为单位的特定时间段:

          public void toast(int millisec, String msg) {
              Handler handler = null;
              final Toast[] toasts = new Toast[1];
              for(int i = 0; i < millisec; i+=2000) {
                  toasts[0] = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
                  toasts[0].show();
                  if(handler == null) {
                      handler = new Handler();
                      handler.postDelayed(new Runnable() {
                          @Override
                          public void run() {
                              toasts[0].cancel();
                          }
                      }, millisec);
                  }
              }
          }
          

          【讨论】:

          • 2000 添加到i 并不能确保时间已经过去.. 您需要改用时钟
          • 你不能指望将2000添加到i会添加2 seconds,这取决于设备速度。如果你想循环直到时间你需要做类似while((System.currentTimeMillis()-startTime)&lt;2000))的事情。这在每种编程语言中都是基本的.. 循环间隔的时间基准永远不会不同。
          【解决方案7】:

          我找到了this answer。虽然有点复杂,但它也允许您创建比 Toast.LENGTH_LONG 更长的 toast。您可能需要将滴答持续时间从 1000 毫秒更改为 500 毫秒。

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

          它是这样工作的:倒计时的通知时间比根据标志显示 toast 的持续时间短,因此如果倒计时没有完成,可以再次显示 toast。如果 toast 仍在屏幕上时再次显示,它将在整个持续时间内保持在那里而不会闪烁。倒计时结束后,即使显示时长未结束,也会取消 toast 以隐藏它。

          即使 toast 必须显示的持续时间短于默认持续时间,这仍然有效:倒计时完成后,显示的第一个 toast 将被取消。

          【讨论】:

            【解决方案8】:

            这个对我来说很好用。

            final Toast mToastToShow;
                        int toastDurationInMilliSeconds = 10000;
                        mToastToShow =  Toast.makeText(getApplicationContext(), "Snapshot Saved Successfully.",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();
            

            倒计时用于在特定持续时间内显示敬酒消息。

            【讨论】:

              【解决方案9】:

              添加到@Senth 的答案,如果您不习惯在多次调用 showToast 方法时累积时间,使用相同的消息:

              private Toast mToastToShow = null;
              String messageBeingDisplayed = "";
              
              /**
               * Show Toast message for a specific duration, does not show again if the message is same
               *
               * @param message     The Message to display in toast
               * @param timeInMSecs Time in ms to show the toast
               */
              public void showToast(String message, int timeInMSecs) {
                  if (mToastToShow != null && message.equals(messageBeingDisplayed)) {
                      Log.d("DEBUG", "Not Showing another Toast, Already Displaying");
                      return;
                  } else {
                      Log.d("DEBUG", "Displaying Toast");
                  }
                  messageBeingDisplayed = message;
                  // Set the toast and duration
                  int toastDurationInMilliSeconds = timeInMSecs;
                  mToastToShow = Toast.makeText(this, message, Toast.LENGTH_LONG);
              
                  // Set the countdown to display the toast
                  CountDownTimer toastCountDown;
                  toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, timeInMSecs /*Tick duration*/) {
                      public void onTick(long millisUntilFinished) {
                          if (mToastToShow != null) {
                              mToastToShow.show();
                          }
                      }
              
                      public void onFinish() {
                          if (mToastToShow != null) {
                              mToastToShow.cancel();
                          }
                          // Making the Toast null again
                          mToastToShow = null;
                          // Emptying the message to compare if its the same message being displayed or not
                          messageBeingDisplayed = "";
                      }
                  };
              
                  // Show the toast and starts the countdown
                  mToastToShow.show();
                  toastCountDown.start();
              }
              

              您现在可以像这样显示 toast 500 毫秒:

              showToast("Not Allowed", 500);
              

              【讨论】:

                【解决方案10】:

                我尝试了不同的方法,这个方法对我有用

                 final Toast mytoast = Toast.makeText(getApplicationContext(), jsonObject.getString("response_message"), Toast.LENGTH_SHORT);
                 mytoast.show();
                
                                        Handler handler = new Handler();
                                        handler.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                mytoast.cancel();
                                            }
                                        }, 5000);// 5 sec
                

                【讨论】:

                  【解决方案11】:

                  我在 droid 端创建了一个类 ToastMessage。

                     public class ToastMessage: IToast
                          {
                              public void LongAlert(string message)
                              {
                                  Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);
                                  toast.Show();
                                  Device.StartTimer(TimeSpan.FromSeconds(0.5), () =>
                                  {               
                                     toast.Cancel();
                                      return false;
                                  });
                              }
                          }
                  

                  我已经创建了接口 IToast

                   public  interface IToast
                      {
                          void LongAlert(string message);
                      }
                  

                  依赖服务调用

                   DependencyService.Get<IToast>().LongAlert("Right Answer");
                  

                  【讨论】:

                    【解决方案12】:

                    接受的答案是正确的,但在我的情况下,可以注意到 toast 闪烁(显示和隐藏)..

                    我有一个解决方案,Toast 不闪烁,您也可以自定义 Toast。

                    开始吧,

                    1) 创建一个名为 LongToast 的类。

                    class LongToast {
                    
                    private LongToast() {}
                    
                    static void makeLongToast(Context context,String text, long durationInMillis) 
                    {
                    
                     final Toast toastMessage = new Toast(context);
                    
                     //Creating TextView.
                     TextView textView = new TextView(context);
                    
                     //Setting up Text Color.
                     textView.setTextColor(Color.parseColor("#fafafa"));
                    
                     //Setting up Text Size.
                     textView.setTextSize(17);
                    
                     //Setting up Toast Message Text.
                     textView.setText(text);
                    
                     //Add padding to Toast message.
                     textView.setPadding(20, 20, 20, 23);
                    
                     //Add Gravity TextView.
                     textView.setGravity(Gravity.CENTER);
                    
                     //Adding TextView into Toast.
                     toastMessage.setView(textView);
                    
                     //Access toast message as View.
                     View toastView = toastMessage.getView();
                    
                     //Set Custom Background on Toast.
                     toastView.setBackgroundResource(R.drawable.test);
                    
                    
                     new CountDownTimer(durationInMillis, 1000)
                      {
                       public void onTick(long millisUntilFinished)
                       {
                        toastMessage.show();
                       }
                      public void onFinish()
                       {
                        toastMessage.cancel();
                       }
                    
                      }.start();
                     }
                    }
                    

                    2) 创建一个可绘制的 xml 用于自定义 Toast。

                    <selector xmlns:android="http://schemas.android.com/apk/res/android">
                    <item>
                     <shape android:shape="rectangle">
                     <solid android:color="#009973"/>
                     <corners android:radius="20dp" />
                     <stroke
                      android:width="4dp"
                      android:color="#01ffc0"
                     />
                    </shape>
                    

                    您可以根据需要自定义吐司。

                    3) 最后调用 Toast。

                    LongToast.makeLongToast(this,"whatever you want",10000);//duration in seconds
                    

                    参考:click here to check

                    谢谢!!。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-12-01
                      • 2016-11-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2020-01-28
                      相关资源
                      最近更新 更多