您的位置:首页 > 其它

常用工具类

2016-03-05 13:59 274 查看
常用工具类
前言:打开大家手上的项目,基本都会有一大批的辅助类,今天特此整理出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:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: