Toast的单例

Android最常用的工具之一。Toast,可是普通的Toast有一个弊端,就是如果一直点击,就是一直在创建新的Toast,就算当前Activity退出了,Toast还是会一直弹,直到所有全部弹完,感觉很烦,就找了一个单例的Toast,用在项目中,感觉还不错。特此记录一下。
原文地址 http://blog.youkuaiyun.com/nugongahou110

首先上重要的代码

public enum ToastMgr {
        builder;
        private View view;
        private TextView tv;
        private Toast toast;
        private Context context;

        /**
         * 初始化Toast
         *
         * @param context
         */
        public void init(Context context) {
            this.context = context;
            view = RelativeLayout.inflate(context, R.layout.toast_layout, null);
            tv = (TextView) view.findViewById(R.id.toast_name);
            toast = new Toast(context);
            toast.setView(view);
        }

        /**
         * 显示在中间Toast
         *
         * @param content
         * @param duration Toast持续时间
         */
        public void display(CharSequence content, int duration) {
            if (content.length() != 0) {
                tv.setText(content);
                toast.setDuration(duration);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }

        /**
         * 显示在底部Toast
         *
         * @param content
         * @param duration Toast持续时间
         */
        public void displayBottom(CharSequence content, int duration) {
            if (content.length() != 0) {
                tv.setText(content);
                toast.setDuration(duration);
                toast.setGravity(Gravity.BOTTOM, 0, 50);
                toast.show();
            }
        }
    }

—上面的代码是初始化整个Toast.然后只需要在Application的oncreate()里初始化一下就好。

 ToastMgr.builder.init(getApplicationContext());

然后再写一个ToastUtils

public class ToastUtils {

    public static void showToast(int resID) {
        showToast(Thinksns.getContext(), Toast.LENGTH_SHORT, resID);
    }

    public static void showToast(String text) {
        showToast(Thinksns.getContext(), Toast.LENGTH_SHORT, text);
    }

    public static void showToast(Context ctx, int resID) {
        showToast(ctx, Toast.LENGTH_SHORT, resID);
    }

    public static void showToast(Context ctx, String text) {
        showToast(ctx, Toast.LENGTH_SHORT, text);
    }

    public static void showLongToast(Context ctx, int resID) {
        showToast(ctx, Toast.LENGTH_LONG, resID);
    }

    public static void showLongToast(int resID) {
        showToast(Thinksns.getContext(), Toast.LENGTH_LONG, resID);
    }

    public static void showLongToast(Context ctx, String text) {
        showToast(ctx, Toast.LENGTH_LONG, text);
    }

    public static void showLongToast(String text) {
        showToast(Thinksns.getContext(), Toast.LENGTH_LONG, text);
    }

    public static void showToast(Context ctx, int duration, int resID) {
        showToast(ctx, duration, ctx.getString(resID));
    }
    /**
     * 带bottom表示在底部显示
     */
    public static void showToastBottom(int resID) {
        showToastBottom(Thinksns.getContext(), Toast.LENGTH_SHORT, resID);
    }

    public static void showToastBottom(String text) {
        showToastBottom(Thinksns.getContext(), Toast.LENGTH_SHORT, text);
    }

    public static void showToastBottom(Context ctx, int resID) {
        showToastBottom(ctx, Toast.LENGTH_SHORT, resID);
    }

    public static void showToastBottom(Context ctx, String text) {
        showToastBottom(ctx, Toast.LENGTH_SHORT, text);
    }

    public static void showLongToastBottom(Context ctx, int resID) {
        showToastBottom(ctx, Toast.LENGTH_LONG, resID);
    }

    public static void showLongToastBottom(int resID) {
        showToastBottom(Thinksns.getContext(), Toast.LENGTH_LONG, resID);
    }

    public static void showLongToastBottom(Context ctx, String text) {
        showToastBottom(ctx, Toast.LENGTH_LONG, text);
    }

    public static void showLongToastBottom(String text) {
        showToastBottom(Thinksns.getContext(), Toast.LENGTH_LONG, text);
    }

    public static void showToastBottom(Context ctx, int duration, int resID) {
        showToastBottom(ctx, duration, ctx.getString(resID));
    }
    /**
     * Toast一个图片
     */
    private static Toast showToastImage(Context ctx, int resID) {
        final Toast toast = Toast.makeText(ctx, "", Toast.LENGTH_SHORT);
        View mNextView = toast.getView();
        if (mNextView != null) mNextView.setBackgroundResource(resID);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        return toast;
    }

    public static void showToastBottom( Context ctx,  int duration,  String text) {
        if (text == null) {
            return;
        } else {
            Thinksns.ToastMgr.builder.displayBottom(text, duration);
        }
    }
    public static void showToast( Context ctx,  int duration,  String text) {
        if (text == null) {
            return;
        }else {
            Thinksns.ToastMgr.builder.display(text, duration);
        }

    }

    /**
     * 在UI线程运行弹出
     */
    public static void showToastOnUiThread(final Activity ctx, final String text) {
        if (ctx != null) {
            ctx.runOnUiThread(new Runnable() {
                public void run() {
                    showToast(ctx, text);
                }
            });
        }
    }

}

最后使用的使用。直接ToastUtils.showToast(“”);就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值