烤面包的时间长度(LENGTH_LONG)和长度(LENGTH_SHORT)

我需要 LENGTH _ LONG 和 LENGTH _ SHORT 的确切持续时间(毫秒)。此外,我需要知道是否与 LENGTH _ LONG 吐司消息的持续时间将有相同的持续时间在任何电话和任何 API 版本。

有人知道期限在哪里吗?,我的意思是定义在毫秒。我知道 LENGTH _ LONG 是一个值为1的 int const。但是我找不到确定的实际持续时间在哪里。

78118 次浏览

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

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

The Toast.LENGTH_SHORT and Toast.LENGTH_LONG are just flags.
You can find here the official android source where these flags are defined:

public class NotificationManagerService extends SystemService {


static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
/** Amount of time (in milliseconds) a toast window can be shown. */
//public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds
static final int SHORT_DELAY = 2000; // 2 seconds


private void scheduleDurationReachedLocked(ToastRecord r)
{
mHandler.removeCallbacksAndMessages(r);
Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
//....
mHandler.sendMessageDelayed(m, delay);
}
}

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.

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

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

LENGTH_SHORT & LENGTH_LONG are mapped to time interval of 1 Second (1000mS) & 5 Seconds (5000mS) respectively,

To see this you need to dig into AOSP source code of Toast. in Toast class time interval is decided based on the FLAG

mParams.hideTimeoutMilliseconds = mDuration == Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;

where

  static final long SHORT_DURATION_TIMEOUT = 5000;
static final long LONG_DURATION_TIMEOUT = 1000;

https://android.googlesource.com/platform/frameworks/base/+/f4bed684c939b0f8809ef404b8609fe4ef849263/core/java/android/widget/Toast.java