ToastUtil

public class ToastUtils {
    private static Toast toast;
    private static TextView mTvToast;
    public static boolean isShow = true;

    public ToastUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void setShow(boolean isShowToast){
        isShow = isShowToast;
    }

    public static void showToastNew(CharSequence text) {
        if(isShow){
            cancelToast();
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getInstance(),text, Toast.LENGTH_SHORT);
            } else {
                toast.setText(text);
            }
            toast.show();
        }
    }

    public static void showToastNew(String content) {
        /*if(content.equals(lastContent))return;
        lastContent = content;*/
        if(isShow){
            /*if (toast == null) {//has already been added to the window manager

            }//end of if*/
            try{
                toast = new Toast(MyApplication.getInstance());
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                View _root = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.toast_custom_common, null);
                mTvToast = (TextView) _root.findViewById(R.id.tvCustomToast);
                toast.setView(_root);
                mTvToast.setText(content);
                toast.show();
            }catch (Exception e){}
        }
    }

    public void showToast(Context ctx, int stringId) {
        showToastNew(ctx.getString(stringId));
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param resId 资源ID:getResources().getString(R.string.xxxxxx);
     * @param duration 单位:毫秒
     */
    public static void show(Context context, int resId, int duration) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, resId, duration);
            } else {
                toast.setText(resId);
            }
            toast.show();
        }
    }

    /**
     * 自定义Toast的View
     * @param context
     * @param message
     * @param duration 单位:毫秒
     * @param view 显示自己的View
     */
    public static void customToastView(Context context, CharSequence message, int duration,View view) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, message, duration);
            } else {
                toast.setText(message);
            }
            if(view != null){
                toast.setView(view);
            }
            toast.show();
        }
    }

    /**
     * 自定义Toast的位置
     * @param context
     * @param message
     * @param duration 单位:毫秒
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void customToastGravity(Context context, CharSequence message, int duration,int gravity, int xOffset, int yOffset) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, message, duration);
            } else {
                toast.setText(message);
            }
            toast.setGravity(gravity, xOffset, yOffset);
            toast.show();
        }
    }

    /**
     * 自定义带图片和文字的Toast,最终的效果就是上面是图片,下面是文字
     * @param context
     * @param message
     * @param iconResId 图片的资源id,如:R.drawable.icon
     * @param duration
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void showToastWithImageAndText(Context context, CharSequence message, int iconResId,int duration,int gravity, int xOffset, int yOffset) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, message, duration);
            } else {
                toast.setText(message);
            }
            toast.setGravity(gravity, xOffset, yOffset);
            LinearLayout toastView = (LinearLayout) toast.getView();
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(iconResId);
            toastView.addView(imageView, 0);
            toast.show();
        }
    }

    /**
     * 自定义Toast,针对类型CharSequence
     * @param context
     * @param message
     * @param duration
     * @param view
     * @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
     * @param gravity
     * @param xOffset
     * @param yOffset
     * @param isMargin true,表示后面的两个参数生效,false,表示不生效
     * @param horizontalMargin
     * @param verticalMargin
     */
    public static void customToastAll(Context context, CharSequence message, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, float verticalMargin) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, message, duration);
            } else {
                toast.setText(message);
            }
            if(view != null){
                toast.setView(view);
            }
            if(isMargin){
                toast.setMargin(horizontalMargin, verticalMargin);
            }
            if(isGravity){
                toast.setGravity(gravity, xOffset, yOffset);
            }
            toast.show();
        }
    }

    /**
     * 自定义Toast,针对类型resId
     * @param context
     * @param resId
     * @param duration
     * @param view :应该是一个布局,布局中包含了自己设置好的内容
     * @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
     * @param gravity
     * @param xOffset
     * @param yOffset
     * @param isMargin true,表示后面的两个参数生效,false,表示不生效
     * @param horizontalMargin
     * @param verticalMargin
     */
    public static void customToastAll(Context context, int resId, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, float verticalMargin) {
        if (isShow){
            if (toast == null) {
                toast = Toast.makeText(context, resId, duration);
            } else {
                toast.setText(resId);
            }
            if(view != null){
                toast.setView(view);
            }
            if(isMargin){
                toast.setMargin(horizontalMargin, verticalMargin);
            }
            if(isGravity){
                toast.setGravity(gravity, xOffset, yOffset);
            }
            toast.show();
        }
    }

    /** 取消吐司显示*/
    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
            toast = null;
            mTvToast = null;
        }
    }
}

 

你可能感兴趣的:(android)