美文网首页
快速toast工具类 解决toast延迟问题

快速toast工具类 解决toast延迟问题

作者: 我是你森哥哥 | 来源:发表于2017-06-16 14:59 被阅读0次

带图片的吐司窗体

快速toast 让其在主线程进行

/**
     * //带图片的吐司窗体
     *
     * @param context 上下文
     * @param message 吐司内容
     * @param imageId 图片ID
     */
    public static void ToastWithImage(final Context context, final Object message, int imageId) {
        Toast toast = Toast.makeText(context.getApplicationContext(), "" + message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        LinearLayout toastView = (LinearLayout) toast.getView();
        if (imageId != 0) {
            ImageView imageCodeProject = new ImageView(context.getApplicationContext());
            imageCodeProject.setImageResource(imageId);
            toastView.addView(imageCodeProject, 0);
            toast.show();
        }
        if (imageId == 0) {
            toast.show();
        }
    }


    /**
     * 快速快速toast  让其在主线程进行
     *
     * @param mContext
     * @param msg
     */
    public static void showToast(final Context mContext, final Object msg) {
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = new SoftReference<Toast>(Toast.makeText(mContext, "", Toast.LENGTH_SHORT));
                } else {
                    if (mToast.get() == null) {
                        mToast = new SoftReference<Toast>(Toast.makeText(mContext, "", Toast.LENGTH_SHORT));
                    } else {
                        mToast.get().setText("" + msg);
                        mToast.get().show();
                    }
                }
            }
        });
    }

相关文章

网友评论

      本文标题:快速toast工具类 解决toast延迟问题

      本文链接:https://www.haomeiwen.com/subject/shcmqxtx.html