平常开发中测试代码,toast和log用的比较多
好处是代码简介方便,一行即可
但这样的代码写的多了,有时候也不易查找
而且影响可读性
于是自己封装了一个Debug管理工具类,
下面直接贴代码:
/** * Created by Administrator on 2017/4/14 0014. * 管理toast和log打印的工具类 */ public class DebugUtil { public static final String TAG = "print"; private DebugUtil() { } /** * 管理log打印 * * @param str */ public static void logD(String str) { if (str != null) { Log.d(TAG, str); } else { Log.d(TAG, "打印参数为空,请检查后再打印执行"); } } //短toast public static void toastShort(Context context, String str) { if (context != null && str != null) { Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } else if (str == null) { Toast.makeText(context, "输入参数为空值,请检查后重新输入", Toast.LENGTH_SHORT).show(); } } //长toast public static void toastLong(Context context, String str) { if (context != null && str != null) { Toast.makeText(context, str, Toast.LENGTH_LONG).show(); } else if (str == null) { Toast.makeText(context, "输入参数为空值,请检查后重新输入", Toast.LENGTH_SHORT).show(); } } //自定义时间 public static void toastTimer(Context context, String str, int durantion) { if (context != null && str != null) { Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } else if (str == null) { Toast.makeText(context, "输入参数为空值,请检查后重新输入", durantion).show(); } } }
这个工具类实现了Toast的弹出和log的打印
同时适配了自定义事件的toast
log打印的话只写了logd,读者可以自己加上其他log