Android项目中使用了的Toast,测试过程中发现有部分手机无法正常显示Toast,尝试了数种解决方案后,问题得以解决,以下为封装的工具类,统一的Toast框,也能避免反复弹出不同的Toast框。

public class LKToastUtil {    private static Toast mToast;    @SuppressLint("StaticFieldLeak")    private static TextView tv_toastMsg;    private static float bottomHeight = 60;    /**     * 快速连续弹Toast     *     * @param msg 需要显示的内容     */    public static void showToastLong(final String msg) {        if (msg.equals("")) {            return;        }        LKBaseApplication.getHandler().post(new Runnable() {            @Override            public void run() {                showToast(msg, Toast.LENGTH_LONG);            }        });    }    /**     * 快速连续弹Toast     *     * @param msg 需要显示的内容     */    public static void showToastShort(final String msg) {        if (msg.equals("")) {            return;        }        LKBaseApplication.getHandler().post(new Runnable() {            @Override            public void run() {                showToast(msg, Toast.LENGTH_SHORT);            }        });    }    /**     * 显示Toast     *     * @param msg      需要显示的内容     * @param duration 显示时长     */    private static void showToast(final String msg, final int duration) {        if (mToast == null) {            mToast = new Toast(LKBaseApplication.getApplication());            @SuppressLint("InflateParams")            View view = LayoutInflater.from(LKBaseApplication.getApplication()).inflate(R.layout.view_toast, null);            tv_toastMsg = view.findViewById(R.id.tv_toastMsg);            mToast.setView(view);        }        tv_toastMsg.setText(msg);        mToast.setDuration(duration);        mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, LKScreenUtil.dp2px(bottomHeight));        mToast.show();    }}

其中LKBaseApplication.getHandler()为Application中定义的全局Handler,如果没有定义的也可以自行传入即可。

更多相关文章

  1. Android Studio 快捷键
  2. 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优
  3. Android(安卓)- 用WebView开发简单的浏览器
  4. 自定义ThreadPoolExecutor
  5. Android(安卓)高德地图,显示地图,并定位当前的位置
  6. Android自定义视图控件
  7. Android(安卓)webView 中loadData方法加载 带中文时出现乱码
  8. 如何定制PopupWindow弹出框及方法介绍
  9. Android(安卓)RoboGuice 使用指南(8):Provider Bindings

随机推荐

  1. android shape的使用
  2. Android中调用webservice小结
  3. Android(安卓)技术专题系列之二 -- telepho
  4. Android使用SQlite数据库
  5. [转]android shape的使用
  6. udacity android 学习笔记: lesson 4 par
  7. android Shader类简介_渲染图像示例
  8. Android位置策略(二)
  9. Android添加工程时出现This template dep
  10. android Audio 应用接口