使用全局toast,防止toast因为消息过多,弹个不停
public class CustomToast {
private static int mDuration = 5000;//toast默认的时间
private static Toast mToast;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
}
};
public static void showToast(Context mContext, String text) {
showToast(mContext, text, mDuration);
}
public static void showToast(Context mContext, int resId) {
showToast(mContext, mContext.getResources().getString(resId), mDuration);
}
public static void showToast(Context mContext, String text, int duration) {
mHandler.removeCallbacks(r);
if (mToast != null)
mToast.setText(text);
else
mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
mHandler.postDelayed(r, duration);
mToast.show();
}
public static void showToast(Context mContext, int resId, int duration) {
showToast(mContext, mContext.getResources().getString(resId), duration);
}
}