安卓 Toast 快速切换

public class MyToastUtil {
    private static Toast mToast;
    public static void showToast(Context context, int resId){
        showToast(context, context.getString(resId));
    }
    public static void showToast(Context context, String msg) {
        if (mToast==null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }else{
            View view = mToast.getView();
            mToast.cancel();
            mToast= new Toast(context);
            mToast.setView(view);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setText(msg);
        }
        mToast.show();
    }
}

亲测有效

 

你可能感兴趣的:(安卓)