天天看点

[视图]Toast优化,同一时间内部重复Toast N次

防止Toast N次,Toast一直不消失.      
private static Toast mToast;      
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
    public void run() {
        mToast.cancel();
    }
};      
//调用showToast提示      
public static void showToast(Context mContext, String text) {
    try {      
//每次调用先把前一个提示给remove掉
        mHandler.removeCallbacks(r);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
        }
        mHandler.postDelayed(r, 2000);
        mToast.show();
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    }
}

      
public static void showToast(Context mContext, int resId) {
    showToast(mContext, mContext.getResources().getString(resId));
}
      
}      

继续阅读