显示android开发 10个常用工具类

android开发工具  时间:2021-02-28  阅读:()

Android开发10个常用工具类

原文出处 张鸿洋的博客

打开大家手上的项目基本都会有一大批的辅助类今天特此整理出10个基本每个项目中都会使用的工具类用于快速开发~~

在此感谢群里给我发项目中工具类的兄弟/姐妹~

1、 日志工具类L.javap ac kage c om.zhy.utils;import android.util.Log;

*Lo g统一管理类public class Lprivate L()

/*cannot be instantiated*/throw new UnsupportedOperationExc eption(c annot be instantiatedpublic static boolean isDebug=true;//是否需要打印bug可以在application的onCreate函数里面初始化private static final String TAG=way

//下面四个是默认tag的函数public static void i(String msg)if(is Debug)

Lo g.i(TAG,m s g);public static void d(String msg)if(is Debug)

Lo g.d(TAG,ms g);public static void e(String msg)if(is Debug)

Lo g.e(TAG,m s g);public static void v(String msg)if(is Debug)

Lo g.v(TAG,ms g);

//下面是传入自定义tag的函数public static void i(String tag,String msg)if(is Debug)

Log.i(tag,ms g);public static void d(String tag,String msg)if(is Debug)

Log.i(tag,ms g);public static void e(String tag,String msg)if(is Debug)

Log.i(tag,ms g);public static void v(String tag,String msg)if(is Debug)

Log.i(tag,ms g);

}

网上看到的类注释上应该原创作者的名字很简单的一个类 网上也有很多提供把日

志记录到SDCard上的不过我是从来没记录过所以引入个最简单的大家可以进行评价是否需要扩充~~

2、 To ast统一管理类p ac kage c om.zhy.utils;import android.c ontent.Context;import android.widget.Toast;

*To ast统一管理类public classTprivate T()

/**cannot be instantiated**/throw new UnsupportedOperationExc eption(c annot be instantiatedpublic static boolean isShow=true;

/**

*短时间显示To as t

*@param context

*@param messagepublic static void showShort(Context context,CharSequence message)if(is Show)

Toast.makeText(c ontext,mes sage,Toast.LENGTH_SHORT).show();

/**

*短时间显示To as t

*@param context

*@param messagepublic static void showShort(Context context, int message)if(is Show)

Toast.makeText(c ontext,mes sage,Toast.LENGTH_SHORT).show();/**

*长时间显示To as t

*@param context

*@param messagepublic static void showLong(Context context,CharSequence message)if(is Show)

Toast.makeText(c ontext,mes s age,Toast.LENGTH_LONG).show();/**

*长时间显示To as t

*@param context

*@param messagepublic static void showLong(Context context, int message)if(is Show)

Toast.makeText(c ontext,mes s age,Toast.LENGTH_LONG).show();/**

* 自定义显示To as t时间

*@param context

*@param message

*@param durationpublic static void show(Context context,CharSequence message, int duration)if(is Show)

Toast.makeText(c ontext,mes s age,duration).show();

/**

* 自定义显示To as t时间

*@param context

*@param message

*@param durationpublic static void show(Context context, int message, int duration)if(is Show)

Toast.makeText(c ontext,mes s age,duration).show();

}

也是非常简单的一个封装能省则省了~~

3、 SharedPreferenc es封装类SPUtilsp ac kage c om.zhy.utils;import java.lang.reflect.Invoc ationTargetExc eption;import java.lang.reflec t.Method;import java.util.Map;import android.c ontent.Context;import android.c ontent.SharedPreferenc es;public class SPUtils

/**

*保存在手机里面的文件名public static final String FILE_NAME=share_data

/**

*保存数据的方法我们需要拿到保存数据的具体类型然后根据类型调用不同的保存方法

*@param context

*@param key

*@param objectpublic static void put(Context c ontext,String key,Object object)

SharedPreferences sp=context.getSharedPreferences(FILE_NAME,

Context.MODE_PRI VATE);

SharedPreferenc es.Editor editor=sp.edit();if(object instanceof String)editor.putString(key, (String)objec t);

} else if(object instanceof Integer)editor.putInt(key, (Integer)objec t);

} else if(object instanceof Boolean)editor.putBoo lean(key, (Boo le an)object);

} else if(object instanceof Float)editor.putFloat(key, (Float)objec t);

} else if(object instanceof Long)

editor.putLong(key, (Long)objec t);

} els eeditor.putString(key,object.toString());

SharedPreferenc es Compat.apply(editor);

/**

*得到保存数据的方法我们根据默认值得到保存的数据的具体类型然后调用相对于的方法获取值

*@param context

*@param key

*@param defaultObject

*@r eturnpublic static Object get(Context context,String key,Object defaultObject)

SharedPreferences sp=context.getSharedPreferences(FILE_NAME,

Context.MODE_PRI VATE);if(defaultObject instanceof String)return sp.getString(key, (String)defaultObjec t);

} else if(defaultObject instanceof Integer)return sp.getInt(key, (Integer)defaultObj ec t);

} else if(defaultObject instanceof Boolean)return sp.getBoo lean(key, (Boo lean)defaultObj ect);

} else if(defaultObject instanceof Float)return sp.getFloat(key, (Float)defaultObjec t);

} else if(defaultObject instanceof Long)

return sp.getLong(key, (Long)defaultObjec t);r eturn null;

/**

*移除某个key值已经对应的值

*@param context

*@param keypublic static void remove(Context context,String key)

SharedPreferences sp=c ontext.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);

SharedPreferenc es.Editor editor=sp.edit();editor.remove(key);

SharedPreferenc es Compat.apply(editor);

/**

*清除所有数据

*@param contextpublic static void clear(Context context)

SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);

SharedPreferenc es.Editor editor=sp.edit();editor.clear();

SharedPreferenc es Compat.apply(editor);

/**

*查询某个key是否已经存在

*@param context

*@param key

*@r eturnpublic static boolean contains(Context context,String key)

SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);return sp.c ontains(key);

/**

*返回所有的键值对

*@param context

*@r eturnpublic static Map String, ?getAll(Context context)

SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);r eturn s p.g etAl l();

/**

*创建一个解决SharedPreferenc es Compat.apply方法的一个兼容类

*@author zhyprivate static class SharedPreferencesCompatprivate static final Method sApp lyMethod=findApp lyMethod();/**

Central美国65折优惠,美国达拉斯机房VPS季付赠送双倍内存

Central美国独立日活动正在进行中,旗下美国达拉斯机房VPS 65折优惠,季付赠送双倍内存(需要发工单),Central租用的Hivelocity的机房,只支持信用卡和加密货币付款,不支持paypal,需要美国独服的可以谨慎入手试试。Central怎么样?Central便宜服务器,Central自称成立于2019年,主营美国达拉斯机房Linux vps、Windows vps、专用服务器和托管...

DiyVM:499元/月香港沙田服务器,L5630*2/16G内存/120G SSD硬盘/5M CN2线路

DiyVM是一家成立于2009年的国人主机商,提供的产品包括VPS主机、独立服务器租用等,产品数据中心包括中国香港、日本大阪和美国洛杉矶等,其中VPS主机基于XEN架构,支持异地备份与自定义镜像,VPS和独立服务器均可提供内网IP功能。商家VPS主机均2GB内存起步,三个地区机房可选,使用优惠码后每月69元起;独立服务器开设在香港沙田电信机房,CN2线路,自动化开通上架,最低499元/月起。下面以...

HostKvm香港VPS七折:$5.95/月KVM-2GB内存/40GB硬盘/500GB月流量

HostKvm是一家成立于2013年的国外主机服务商,主要提供VPS主机,基于KVM架构,可选数据中心包括日本、新加坡、韩国、美国、俄罗斯、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。商家本月针对香港国际机房提供特别7折优惠码,其他机房全场8折,优惠后2G内存香港VPS每月5.95美元起,支持使用PayPal或者支付宝付款。下面以香港国际(HKGlobal)为...

android开发工具为你推荐
weipin唯品金融是什么?大家基本都怎么用呢?站长故事爱迪生发明东西的故事快速美白好方法有什么变白的好方法怎么在qq空间里添加背景音乐怎么在QQ空间里插入背景音乐??伪静态怎么做伪静态?今日热点怎么删除怎样删除实时热点qq怎么发邮件如何通过QQ发送邮件安全漏洞计算机一般存在哪些安全漏洞?怎么上传音乐怎么上传音乐到网上gbk编码表gbk内码表怎么看
www二级域名 政务和公益机构域名注册管理中心 免费cn域名注册 Oray域名注册服务商 江西服务器租用 什么是二级域名 万网域名管理 美国主机排名 流媒体服务器 60g硬盘 免费网站监控 香港新世界电讯 免费全能空间 亚洲小于500m 全站静态化 域名转向 新天域互联 帽子云 cn3 umax120 更多