How can I show a toast for a specific duration?

2019-01-16 16:50发布

This is the way I have to show the Toast for 500 milliseconds. Though, it's showing more than a second.

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

How can I show Toast only for 500 milliseconds?

12条回答
叛逆
2楼-- · 2019-01-16 17:15

Can't do what you are asking with the standard Toasts. Perhaps you should think about integrating a 3rd party library that gives you better Toast options (named Crouton). I haven't used it myself, but people seem to like it.

You can't control the length of Toasts in the standard OS.

Crouton link: https://github.com/keyboardsurfer/Crouton

查看更多
beautiful°
3楼-- · 2019-01-16 17:17

You can also modify the pattern of the Toast.LENGTH_LONG example:

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

Remembering that the pattern has a duration of 1 second

查看更多
唯我独甜
4楼-- · 2019-01-16 17:19

Adding to @Senth's answer, if you don't wont to accumulate the time when you call the showToast method multiple times, with the same Message:

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

You can display toast now for 500 ms like this:

showToast("Not Allowed", 500);
查看更多
Ridiculous、
5楼-- · 2019-01-16 17:20

This one is working fine for me.

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

the countdown is used to display a toast message for a specific duration.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-16 17:20

I don't believe this can be done, you can only use Toast.LENGTH_LONG or Toast.LENTH_SHORT you can't define your know speed.

查看更多
祖国的老花朵
7楼-- · 2019-01-16 17:24

I tried different method and this method works for me

 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
查看更多
登录 后发表回答