我的解决思路:
private static long toastTime;//记录Toast展示时间
private static String oldToastMsg;//记录上次Toast消息
public Toast mToast;
toastTime、用于记录Toast.show的时间、 oldToastMsg记录上次Toast消息、用于是否为相同消息的判断
public void showToast(String msg) {
if ((System.currentTimeMillis() - toastTime) <= 2000 && msg.equals(oldToastMsg)) {
return;
}
if (mToast == null) {
mToast = new Toast(this);
} else {
mToast.cancel();
mToast = new Toast(this);
}
oldToastMsg = msg;
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.CENTER, 0, 0);
TextView txtToast = new TextView();
txtToast.setText(msg);
mToast.setView(toastView);
mToast.show();
toastTime = System.currentTimeMillis();
}
Toast.LENGTH_SHORT==2000毫秒、LONG==3500毫秒;
好了、不会有什么问题了、按照自己需求改变Toast样式就行了!