显示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();/**

快云科技,美国VPS 2H5G独享20M 仅售19.8/月  年付仅需148

快云科技已稳步运行进两年了 期间没出现过线路不稳 客户不满意等一系列问题 本司资质齐全 持有IDC ICP ISP等正规手续 有独特的网站设计理念 在前几天刚是参加过魔方系统举行的设计大赛拿获最佳设计奖第一名 本公司主营产品 香港弹性云服务器,美国vps和日本vps,香港物理机,国内高防物理机以及美国日本高防物理机 2020年的国庆推出过一款香港的回馈用户特惠机 已作为传家宝 稳定运行 马上又到了...

Fiberia.io:$2.9/月KVM-4GB/50GB/2TB/荷兰机房

Fiberia.io是个新站,跟ViridWeb.com同一家公司的,主要提供基于KVM架构的VPS主机,数据中心在荷兰Dronten。商家的主机价格不算贵,比如4GB内存套餐每月2.9美元起,采用SSD硬盘,1Gbps网络端口,提供IPv4+IPv6,支持PayPal付款,有7天退款承诺,感兴趣的可以试一试,年付有优惠但建议月付为宜。下面列出几款主机配置信息。CPU:1core内存:4GB硬盘:...

百纵科技:美国独立服务器租用/高配置;E52670/32G内存/512G SSD/4IP/50M带宽,999元/月

百纵科技怎么样?百纵科技国人商家,ISP ICP 电信增值许可证的正规公司,近期上线美国C3机房洛杉矶独立服务器,大带宽/高配置多ip站群服务器。百纵科技拥有专业技术售后团队,机器支持自动化,自助安装系统 重启,开机交付时间 30分钟内交付!美国洛杉矶高防服务器配置特点: 硬件配置高 线路稳定 洛杉矶C3机房等级T4 平价销售,支持免费测试,美国独服适合做站,满意付款。点击进入:百纵科技官方网站地...

android开发工具为你推荐
可以发外链的论坛有直接能带链接的论坛?真正免费的网络电话谁有真正免费的网络电话??在线漏洞检测如何查看网站的漏洞?邮箱打不开怎么办我的邮箱打不开怎么办淘宝店推广淘宝店铺推广有哪些渠道?网店推广网站可以介绍几个可以做店铺推广的网站吗?bt封杀现在是全面封杀BT下载了吗?现在都找不到BT下载影片了srv记录SRV记录的简介域名库想自己买一个域名,然后自己做一个网站,挂上去。请问基本流程是什么样的?cisco防火墙思科路由器上自带防火墙功能吗?
空间域名 双线服务器租用 php主机租用 河南vps 美国主机排名 10t等于多少g 香港机房托管 12306抢票攻略 ixwebhosting 国外免费空间 好看qq空间 个人域名 web服务器架设 网络空间租赁 酷番云 独立主机 双线空间 镇江高防 服务器硬件配置 网络速度 更多