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;        }    }}

 

更多相关文章

  1. SwipeRefreshLayout使用
  2. scrollView 在中间,上下各有一个固定控件的布局
  3. Android(安卓)VideoView
  4. 自定义数据适配器
  5. Android(安卓)仿京东分类页面
  6. Android之ListView优化
  7. android notification应用
  8. Android(安卓)UI界面之TableLayout
  9. 一个旋转layout布局文件

随机推荐

  1. Android(安卓)Environment 常量含义
  2. Android(安卓)Wear Preview - 设计规范(De
  3. 针对Android(安卓)平板的海豚浏览器正式
  4. Android(安卓)LifecycleObserver
  5. Android--ListView 分割线
  6. android root后数据安全
  7. android EditText 不自动获取焦点并弹出
  8. android API版本对应的系统版本及Android
  9. Android关机重启
  10. android仿漫画源码、抽奖转盘、Google相