1. 程式人生 > >Toast工具類,App退出時Toast同時銷燬

Toast工具類,App退出時Toast同時銷燬

public class UIUtils {
    private static Toast mToast;

    public static void showToastShort(Context context, String msg) {
        if (mToast == null) {
            if (msg != null && msg.length() > 13) {
                mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            } else {
                mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            }
            mToast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

     /**
     * Activity銷燬時在onDestory中呼叫
     * 即可修復APP退出時仍顯示toast的問題
     */
    public static void cancleToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

}