目前 Toast 只存在两个持续时间:Toast.LENGTH_SHORT
和Toast.LENGTH_LONG
...
但是,如果您想在 Android 中增加 Toast 的持续时间怎么办?
这是我想出并想分享的一个技巧:
public void createToast(Context context, String s) {
int duration = Toast.LENGTH_LONG;
final Toast toast = Toast.makeText(context, s, duration);
toast.show();
new CountDownTimer(5000, 1000)
{
public void onTick(long millisUntilFinished) {
if (toast.getView().getWindowToken() != null)
toast.show();
else
cancel();
}
public void onFinish() {
if (toast.getView().getWindowToken() !=null)
toast.show();
else
cancel();
}
}.start();
}
如果您想要更长的吐司时间,只需增加CountDownTimer
.
请注意线条
if (toast.getView().getWindowToken !=null)
如果windowToken
toast 的 为空,这几乎是说 Toast 已经离开了视图,(即 toast 被取消了)。
我在网上找到了一些解决方案来增加敬酒的持续时间,但是如果敬酒已被用户解雇,我找不到一个可以保留解雇的解决方案。因此,我将上述内容拼凑在一起以保留正常的 Toast 功能。
让我知道你的想法!