美文网首页
ToastUtil工具类

ToastUtil工具类

作者: 小婷婷tt | 来源:发表于2018-05-16 14:00 被阅读0次

步骤一:新建ToastUtil类

public class ToastUtil {

private static ToastUtilmToastUtil;

private ContextmContext;

private ToastmToast;

public static ToastUtil init(Context context) {

if (mToastUtil ==null) {

mToastUtil =new ToastUtil(context);

}

return mToastUtil;

}

public static ToastUtil getInstance() {

if (mToastUtil ==null) {

throw new NullPointerException("请在Application中初始化ToastUtil");

}

return mToastUtil;

}

private ToastUtil(Context context) {

mContext = context;

init();

}

/**

    * toast string消息,时间2秒

    *

    * @param msg

    */

    public void showToast(String msg) {

mToast.setText(msg);

mToast.setDuration(Toast.LENGTH_SHORT);

mToast.show();

}

/**

    * toast string消息,时间2秒

    *

    * @param msg

    */

    public void showToast(String msg,int time) {

mToast.setText(msg);

mToast.setDuration(time);

mToast.show();

}

/**

    * toast string消息,时间2秒

    *

    * @param resId

    */

    public void showToast(int resId) {

mToast.setText(mContext.getResources().getString(resId));

mToast.setDuration(Toast.LENGTH_SHORT);

mToast.show();

}

private void init() {

LogUtil.e("ToastUtil","初始化ToastUtil");

mToast = Toast.makeText(mContext,"", Toast.LENGTH_SHORT);

}

}

步骤二:在Application类的onCreate()方法里调用

ToastUtil.init(getApplicationContext());

相关文章

  • 常用工具类

    BaseApp ToastUtil(Toast 工具类) MyAppGlideModule(自动缓存图片的工具类,...

  • 常用的工具类

    BaseApp ToastUtil(Toast 工具类) MyAppGlideModule(自动缓存图片的工具类,...

  • 工具类常用(夜间模式)

    BaseApp ToastUtil(Toast 工具类) HttpUtils、RxUtils、Constants(...

  • ToastUtil工具类

    步骤一:新建ToastUtil类 public class ToastUtil { private static ...

  • ToastUtil工具类

    自定义toast,解决系统toast连续弹出,长时间不消失的问题

  • ToastUtil工具类

    一.使用ToastUtil 封装的目的:不让业务直接操作具体的框架,而操作我们的API,这样方便后面重构。 在An...

  • Android ToastUtil

    ToastUtil(工具类) APP(初始化) AndroidManifest.xml(清单文件)

  • Toast封装(2018.10.15)

    1. ToastUtil类 2. 调用 ToastUtil.show(activity,activity.getS...

  • Android-ToastUtil提示框

    1、调用方法 R.string.regist_not_empty_pwd:文字描述 2、创建ToastUtil类

  • ToastUtil

    public class ToastUtil { public static ToastmToast; publi...

网友评论

      本文标题:ToastUtil工具类

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