Android对Toast简单封装之ToastMaker

今天再来记录一个工具类,Toast,每个项目都会用到的这个东东,短暂出现的通知,它们只显示几秒钟就会消失。Toast 不会获取焦点,并且是非模态的,所以它们不会打断当前活动的应用程序,很友好的东东,现在来对它进行一个简单的封装喽~

先放个图,好喜欢今朝和祈,相依为命的生活。O(∩_∩)O哈哈哈~


Android对Toast简单封装之ToastMaker_第1张图片
截图取自网络

有个网站地址,里面不少框架搭建的东西,可供平常项目框架搭建的参考噢~
http://code.taobao.org/p/frameworkutils/diff/3/trunk/AndroidwithXutilsFramework/app/src

我这里对它里面的ToastMaker稍作点修改,当我们点击按钮触发Toast时,若前一个Toast提示还没消失,又触发了新的Toast提示了,我这边是直接修改提示文字的,感觉用户体验会更好,不然连续点击N次触发Toast,那提示得持续多久哇!

/**
 * 对app中所有的toast进行管理
 * Created by pengyn on 2016/1/5.
 */
public class ToastMaker {

    private static Toast sToast;
    private static TextView sContentTv;

    public static void showShortToast(String msg) {
        showCustomToast(BaseApplication.getInstance(), msg, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(int msgId) {
        showCustomToast(BaseApplication.getInstance(), msgId, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(String msg) {
        showCustomToast(BaseApplication.getInstance(), msg, Toast.LENGTH_LONG);
    }

    public static void showLongToast(int msgId) {
        showCustomToast(BaseApplication.getInstance(), msgId, Toast.LENGTH_LONG);
    }

    /**
     * 创建运行在UI线程中的Toast
     *
     * @param activity
     * @param msg
     */
    public static void showToastInUiThread(final Activity activity, final String msg) {
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    showCustomToast(activity, msg);
                }
            });
        }
    }

    public static void showToastInUiThread(final Activity activity, final int stringId) {
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    showCustomToast(activity, stringId);
                }
            });
        }
    }

    private static void showCustomToast(Context context, int msgId) {
        final String msg = context.getResources().getString(msgId);
        showCustomToast(context, msg);
    }

    private static void showCustomToast(Context context, String msg) {
        showCustomToast(context, msg, Toast.LENGTH_SHORT);
    }

    private static void showCustomToast(Context context, int msgId, int duration) {
        final String msg = context.getResources().getString(msgId);
        showCustomToast(context, msg, duration);
    }

    private static void showCustomToast(final Context context, final String msg, final int duration) {

        if (context == null) {
            return;
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            showToast(context, msg, duration);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    showToast(context, msg, duration);
                }
            });
        }
    }

    private static void showToast(Context context, String msg, int duration) {

        if (null != context) {

            if (sToast == null) {
                sToast = new Toast(context);
                LayoutInflater inflater = LayoutInflater.from(context);
                View layout = inflater.inflate(R.layout.toast, null);
                sContentTv = (TextView) layout.findViewById(R.id.tv_toast_content);
                sContentTv.setText(msg);

                sToast.setGravity(Gravity.CENTER, 0, BaseApplication.getInstance().screenH / 4);
                sToast.setDuration(duration);
                sToast.setView(layout);
            }else {
                sContentTv.setText(msg);
            }

            sToast.show();
        }
    }
}

代码比较容易看懂,然后有需要什么带图片的Toast什么的,就自己定义根据需求修改啦,我反正感觉这样子用挺好用的。

2017.1.20 附上一个不错常用的 Toast 封装Toasty

你可能感兴趣的:(Android对Toast简单封装之ToastMaker)