What is the duration of a Toast LENGTH_LONG and LENGTH_SHORT

橙三吉。 提交于 2019-11-26 14:02:29

问题


I need the exact duration of LENGTH_LONG and LENGTH_SHORT in milliseconds (ms). Also I need to know if the duration of Toast message with LENGTH_LONG will have the same duration in any phone and with any API version.

Does someone know where is the duration defined ?, I mean defined in ms . I know that LENGTH_LONG is some int const with value 1. But I could not find where is the actual duration defined.


回答1:


Answered here. Like you mentioned Toast.LENGTH_SHORT and Toast.LENGTH_LONG are not in ms but 0 or 1.

The actual durations are:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds



回答2:


Here the official android source where these flags are defined:

public class NotificationManagerService extends SystemService {

    static final int LONG_DELAY = 3500; // 3.5 seconds
    static final int SHORT_DELAY = 2000; // 2 seconds

    private void scheduleTimeoutLocked(ToastRecord r){
        mHandler.removeCallbacksAndMessages(r);
        Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
        long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
        mHandler.sendMessageDelayed(m, delay);
    }
}



回答3:


its true that we are not allowed to change the duration of the Toast. But if you are searching for an alternative and you really need to do this with a Toast, then you can try this.

Keep another duplicate toast in the next line

Ex:

Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();

The user won't detect any change in transitions between 2 toasts.

Thanks.




回答4:


You need set the duration override, with setDuration in the action, example:

int s = 6000; // milisegundo    
Snackbar.make(coordinatorLayout, "This is my Snackbar", Snackbar.LENGTH_LONG).setDuration(s)
.show();



回答5:


I am wondering that why do you not use method setDuration() supported in Toast.java class???

 /**
 * Set how long to show the view for.
 * @see #LENGTH_SHORT
 * @see #LENGTH_LONG
 */
public void setDuration(@Duration int duration) {
    mDuration = duration;
}



回答6:


Through trial and error I have found Toast.LENGTH_LONG lasts very close to 2500ms



来源:https://stackoverflow.com/questions/7965135/what-is-the-duration-of-a-toast-length-long-and-length-short

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!