在Android系统7.0及以上机型,快速点击显示toast,发现前面的toast覆盖显示,后面的几个Toast就不再显示。
其处理方法是在每次toast的时候,如果tosat isShowing,就调用cancle方法把之前的close
/** * Close the view if it's showing, or don't show it if it isn't showing yet. * You do not normally have to call this. Normally view will disappear on its own * after the appropriate duration. */ public void cancel() { mTN.cancel(); }
这里按照此方法封装一下就可以解决了
private Toast mShowingToast; // 主线程的Handler对象 private Handler mHandler = new Handler(Looper.getMainLooper()); public Toast showToastText(CharSequence text) { if (!canToast) { return null; } if (TextUtils.isEmpty(text)) { return mToast; } if (mShowingToast == null) { mShowingToast = Toast.makeText(this, "", Toast.LENGTH_SHORT); } mShowingToast.cancel(); mShowingToast.setText(text); //mShowingToast.setDuration(Toast.LENGTH_SHORT); mHandler.postDelayed(new Runnable() { @Override public void run() { mShowingToast.show(); } }, 300); return mShowingToast; }
有问题欢迎指正
文章来源: https://blog.csdn.net/earnking/article/details/90475215