如何在特定时间内出示祝酒词?

问题描述 投票:21回答:13

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

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

如何才能在500毫秒内显示Toast

android toast android-toast
13个回答
65
投票

这是不可能做到的。要显示比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);

0
投票

我在机器人方面创建了一个类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");

0
投票

您还可以修改Toast.LENGTH_LONG示例的模式:

Toast.makeText(getBaseContext(),"your message",Toast.LENGTH_LONG*3).show();

记住该模式的持续时间为1秒


0
投票

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

我得到了一个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)最后叫吐司。

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

ref:click here to check

谢谢!!。


-3
投票
Toast.makeText(LiveChar.this,"Typing",Toast.LENGTH_SHORT);

这是你唯一的方法..


3
投票

添加到@ 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 == 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();
}

你可以现在显示吐司500毫秒,如下所示:

showToast("Not Allowed", 500);

2
投票

我找到了this answer。虽然有点复杂,但它也允许你创建比Toast.LENGTH_LONG更长的祝酒词。您可能需要将打勾持续时间从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以隐藏它。

即使吐司必须显示的持续时间短于默认持续时间,此功能仍然有效:倒计时结束时,只显示第一个显示的吐司。


1
投票

不能用标准的Toast做你要求的。也许您应该考虑集成第三方库,为您提供更好的Toast选项(名为Crouton)。我自己没有用过它,但人们似乎喜欢它。

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

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


1
投票

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


1
投票

这个对我来说很好。

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

倒计时用于显示特定持续时间的Toast消息。


0
投票

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


0
投票

先试试吧。这会以特定时间段设置干杯,以毫秒为单位:

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

0
投票

我尝试了不同的方法,这种方法适合我

 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
© www.soinside.com 2019 - 2024. All rights reserved.