android 中单例toast工具类

来源:互联网 发布:仓库用的软件 编辑:程序博客网 时间:2024/05/17 23:27

/**
* @ 创建者 zsh
* @ 创建时间 2017/1/3 10:43
* @ 描述 ${toast的工具类}
*/

public class ToastUtils {    private volatile static Toast mToast = null;    private  static  Toast getToastInstance(Context context) {        if (mToast == null) {            synchronized (ToastUtils.class) {                if (mToast == null) {                    mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);                }            }        }        return mToast;    }    /**     * 长时间的toast     * @param text toast的内容     */    public static void showLongToast(Context context,String text) {        getToastInstance(context).setText(text);        mToast.show();    }    /**     * 短时间的toast     * @param text toast的内容     */    public static void showShortToast(Context context,String text) {        getToastInstance(context).setText(text);        mToast.setDuration(Toast.LENGTH_SHORT);        mToast.show();    }}
1 0
原创粉丝点击