常用工具类
2016-03-05 13:59
274 查看
常用工具类
前言:打开大家手上的项目,基本都会有一大批的辅助类,今天特此整理出10个基本每个项目中都会使用的工具类,用于快速开发~~
一:日志工具类L.java
eg
二:Toast统一管理类
eg:
三:SharedPreferences封装类SPUtils
eg:
四:单位转换类 DensityUtils
eg:
前言:打开大家手上的项目,基本都会有一大批的辅助类,今天特此整理出10个基本每个项目中都会使用的工具类,用于快速开发~~
一:日志工具类L.java
eg
二:Toast统一管理类
eg:
public class ToastManager { private static boolean isShow=false; private ToastManager(){ //问题: throw new UnsupportedOperationException("cannot be instantiated"); } /** * 短时间显示 */ public static void showShort(Context context,CharSequence text){ if(isShow){ Toast.makeText(context, text, Toast.LENGTH_SHORT); } } /** * 长时间显示 */ public static void showLong(Context context,CharSequence text){ if(isShow){ Toast.makeText(context, text, Toast.LENGTH_LONG); } } /** * 短时间显示 */ public static void showShort(Context context,int text){ if(isShow){ Toast.makeText(context, text, Toast.LENGTH_SHORT); } } /** * 长时间显示 */ public static void showLong(Context context,int text){ if(isShow){ Toast.makeText(context, text, Toast.LENGTH_LONG); } } /** * 自定义显示时间 */ public static void show(Context context,CharSequence text,int duration){ if(isShow){ Toast.makeText(context, text, duration); } } /** * 自定义显示时间 */ public static void show(Context context,int text,int duration){ if(isShow){ Toast.makeText(context, text, duration); } } }
三:SharedPreferences封装类SPUtils
eg:
public class SPUtils { private static final String FILE_NAME="config"; private SPUtils(){ } /** * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 * 键值对==键和值 因此存储时要有键和值 * @param context * @param key 具体值对应的键 * @param object 具体值 */ public static void put(Context context,String key,Object object){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); if(object instanceof String){ editor.putString(key, (String) object); }if(object instanceof Integer){ editor.putInt(key, (Integer) object); }if(object instanceof Boolean){ editor.putBoolean(key, (Boolean) object); }if(object instanceof Float){ editor.putFloat(key, (Float) object); }if(object instanceof Long){ editor.putLong(key, (Long) object); }else{ editor.putString(key, object.toString()); } SharedPreferencesCompot.apply(editor); } /** * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 * @param context 上下文 * @param key 要取值对应的键 * @param defaultObject 默认值确认具体类型 * @return */ public static Object get(Context context,String key,Object defaultObject){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); if(defaultObject instanceof String){ sp.getString(key, (String) defaultObject); }if(defaultObject instanceof Integer){ sp.getInt(key, (Integer) defaultObject); }if(defaultObject instanceof Boolean){ sp.getBoolean(key, (Boolean) defaultObject); }if(defaultObject instanceof Float){ sp.getFloat(key, (Float) defaultObject); }if(defaultObject instanceof Long){ sp.getLong(key, (Long) defaultObject); } return null; } /** * 移除某个key值已经对应的值 * @param context * @param key */ public static void remove(Context context,String key){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.remove(key); SharedPreferencesCompot.apply(editor); } /** * 清除数据 * @param context * @param key */ public static void clear(Context context,String key){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.clear(); SharedPreferencesCompot.apply(editor); } /** * 判断key键对应的值是否存在 * @param context * @param key */ public static boolean contains(Context context,String key){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.contains(key); } /** * 返回所有键对应的值 * @param context * @param key */ public static Map<String,?> getAll(Context context){ SharedPreferences sp=context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.getAll(); } /** * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类 * @author Administrator * */ private static class SharedPreferencesCompot{ private static final Method isApplayMethod=findMethod(); private static Method findMethod() { // TODO Auto-generated method stub try { Class cls = SharedPreferences.Editor.class; return cls.getMethod("apply"); } catch (Exception e) { // TODO: handle exception } return null; } public static void apply(SharedPreferences.Editor editor){ try { if(isApplayMethod!=null){ isApplayMethod.invoke(editor); return; } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } editor.commit(); } } }
四:单位转换类 DensityUtils
eg:
相关文章推荐
- Oracle 11g R2 用exp无法导出空表解决方案
- Java中String类的内存分配
- 查看Ubuntu版本
- 在Ubuntu 14.10中借用Windows的字体
- Non-blocking user input in loop without ncurses.
- 注意Hibernate4在开发当中的一些改变
- ubuntu安装jre
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
- HDOJ 1507 Uncle Tom's Inherited Land*(二分图匹配+构图)
- [LeetCode] Meeting Rooms II 会议室之二
- listView的优化学习理解
- convertView和viewholder
- 今日开讲—— easyui-combobox动态赋值
- CALayer图层
- 剑指offer --- 二叉树中和为某一数的路径
- Java中的静态绑定和动态绑定
- 盲人可以玩的魔方
- PHP与Web服务器的链接原理 涉及 php-fpm原理
- LeetCode 51 - N-Queens II
- Bellman-Ford算法的队列优化