Android快速开发系列 10个常用工具类.docx

上传人:b****3 文档编号:10547653 上传时间:2023-05-26 格式:DOCX 页数:46 大小:31.48KB
下载 相关 举报
Android快速开发系列 10个常用工具类.docx_第1页
第1页 / 共46页
Android快速开发系列 10个常用工具类.docx_第2页
第2页 / 共46页
Android快速开发系列 10个常用工具类.docx_第3页
第3页 / 共46页
Android快速开发系列 10个常用工具类.docx_第4页
第4页 / 共46页
Android快速开发系列 10个常用工具类.docx_第5页
第5页 / 共46页
Android快速开发系列 10个常用工具类.docx_第6页
第6页 / 共46页
Android快速开发系列 10个常用工具类.docx_第7页
第7页 / 共46页
Android快速开发系列 10个常用工具类.docx_第8页
第8页 / 共46页
Android快速开发系列 10个常用工具类.docx_第9页
第9页 / 共46页
Android快速开发系列 10个常用工具类.docx_第10页
第10页 / 共46页
Android快速开发系列 10个常用工具类.docx_第11页
第11页 / 共46页
Android快速开发系列 10个常用工具类.docx_第12页
第12页 / 共46页
Android快速开发系列 10个常用工具类.docx_第13页
第13页 / 共46页
Android快速开发系列 10个常用工具类.docx_第14页
第14页 / 共46页
Android快速开发系列 10个常用工具类.docx_第15页
第15页 / 共46页
Android快速开发系列 10个常用工具类.docx_第16页
第16页 / 共46页
Android快速开发系列 10个常用工具类.docx_第17页
第17页 / 共46页
Android快速开发系列 10个常用工具类.docx_第18页
第18页 / 共46页
Android快速开发系列 10个常用工具类.docx_第19页
第19页 / 共46页
Android快速开发系列 10个常用工具类.docx_第20页
第20页 / 共46页
亲,该文档总共46页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Android快速开发系列 10个常用工具类.docx

《Android快速开发系列 10个常用工具类.docx》由会员分享,可在线阅读,更多相关《Android快速开发系列 10个常用工具类.docx(46页珍藏版)》请在冰点文库上搜索。

Android快速开发系列 10个常用工具类.docx

Android快速开发系列10个常用工具类

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

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

1、日志工具类L.java

[java] viewplaincopy

1.package com.zhy.utils;  

2.  

3.import android.util.Log;  

4.  

5./** 

6. * Log统一管理类 

7. *  

8. *  

9. *  

10. */  

11.public class L  

12.{  

13.  

14.    private L()  

15.    {  

16.        /* cannot be instantiated */  

17.        throw new UnsupportedOperationException("cannot be instantiated");  

18.    }  

19.  

20.    public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化  

21.    private static final String TAG = "way";  

22.  

23.    // 下面四个是默认tag的函数  

24.    public static void i(String msg)  

25.    {  

26.        if (isDebug)  

27.            Log.i(TAG, msg);  

28.    }  

29.  

30.    public static void d(String msg)  

31.    {  

32.        if (isDebug)  

33.            Log.d(TAG, msg);  

34.    }  

35.  

36.    public static void e(String msg)  

37.    {  

38.        if (isDebug)  

39.            Log.e(TAG, msg);  

40.    }  

41.  

42.    public static void v(String msg)  

43.    {  

44.        if (isDebug)  

45.            Log.v(TAG, msg);  

46.    }  

47.  

48.    // 下面是传入自定义tag的函数  

49.    public static void i(String tag, String msg)  

50.    {  

51.        if (isDebug)  

52.            Log.i(tag, msg);  

53.    }  

54.  

55.    public static void d(String tag, String msg)  

56.    {  

57.        if (isDebug)  

58.            Log.i(tag, msg);  

59.    }  

60.  

61.    public static void e(String tag, String msg)  

62.    {  

63.        if (isDebug)  

64.            Log.i(tag, msg);  

65.    }  

66.  

67.    public static void v(String tag, String msg)  

68.    {  

69.        if (isDebug)  

70.            Log.i(tag, msg);  

71.    }  

72.}  

网上看到的类,注释上应该原创作者的名字,很简单的一个类;网上也有很多提供把日志记录到SDCard上的,不过我是从来没记录过,所以引入个最简单的,大家可以进行评价是否需要扩充~~

2、Toast统一管理类 

[java] viewplaincopy

1.package com.zhy.utils;  

2.  

3.import android.content.Context;  

4.import android.widget.Toast;  

5.  

6./** 

7. * Toast统一管理类 

8. *  

9. */  

10.public class T  

11.{  

12.  

13.    private T()  

14.    {  

15.        /* cannot be instantiated */  

16.        throw new UnsupportedOperationException("cannot be instantiated");  

17.    }  

18.  

19.    public static boolean isShow = true;  

20.  

21.    /** 

22.     * 短时间显示Toast 

23.     *  

24.     * @param context 

25.     * @param message 

26.     */  

27.    public static void showShort(Context context, CharSequence message)  

28.    {  

29.        if (isShow)  

30.            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  

31.    }  

32.  

33.    /** 

34.     * 短时间显示Toast 

35.     *  

36.     * @param context 

37.     * @param message 

38.     */  

39.    public static void showShort(Context context, int message)  

40.    {  

41.        if (isShow)  

42.            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  

43.    }  

44.  

45.    /** 

46.     * 长时间显示Toast 

47.     *  

48.     * @param context 

49.     * @param message 

50.     */  

51.    public static void showLong(Context context, CharSequence message)  

52.    {  

53.        if (isShow)  

54.            Toast.makeText(context, message, Toast.LENGTH_LONG).show();  

55.    }  

56.  

57.    /** 

58.     * 长时间显示Toast 

59.     *  

60.     * @param context 

61.     * @param message 

62.     */  

63.    public static void showLong(Context context, int message)  

64.    {  

65.        if (isShow)  

66.            Toast.makeText(context, message, Toast.LENGTH_LONG).show();  

67.    }  

68.  

69.    /** 

70.     * 自定义显示Toast时间 

71.     *  

72.     * @param context 

73.     * @param message 

74.     * @param duration 

75.     */  

76.    public static void show(Context context, CharSequence message, int duration)  

77.    {  

78.        if (isShow)  

79.            Toast.makeText(context, message, duration).show();  

80.    }  

81.  

82.    /** 

83.     * 自定义显示Toast时间 

84.     *  

85.     * @param context 

86.     * @param message 

87.     * @param duration 

88.     */  

89.    public static void show(Context context, int message, int duration)  

90.    {  

91.        if (isShow)  

92.            Toast.makeText(context, message, duration).show();  

93.    }  

94.  

95.}  

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

3、SharedPreferences封装类SPUtils

[java] viewplaincopy

1.package com.zhy.utils;  

2.  

3.import java.lang.reflect.InvocationTargetException;  

4.import java.lang.reflect.Method;  

5.import java.util.Map;  

6.  

7.import android.content.Context;  

8.import android.content.SharedPreferences;  

9.  

10.public class SPUtils  

11.{  

12.    /** 

13.     * 保存在手机里面的文件名 

14.     */  

15.    public static final String FILE_NAME = "share_data";  

16.  

17.    /** 

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

19.     *  

20.     * @param context 

21.     * @param key 

22.     * @param object 

23.     */  

24.    public static void put(Context context, String key, Object object)  

25.    {  

26.  

27.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

28.                Context.MODE_PRIVATE);  

29.        SharedPreferences.Editor editor = sp.edit();  

30.  

31.        if (object instanceof String)  

32.        {  

33.            editor.putString(key, (String) object);  

34.        } else if (object instanceof Integer)  

35.        {  

36.            editor.putInt(key, (Integer) object);  

37.        } else if (object instanceof Boolean)  

38.        {  

39.            editor.putBoolean(key, (Boolean) object);  

40.        } else if (object instanceof Float)  

41.        {  

42.            editor.putFloat(key, (Float) object);  

43.        } else if (object instanceof Long)  

44.        {  

45.            editor.putLong(key, (Long) object);  

46.        } else  

47.        {  

48.            editor.putString(key, object.toString());  

49.        }  

50.  

51.        SharedPreferencesCompat.apply(editor);  

52.    }  

53.  

54.    /** 

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

56.     *  

57.     * @param context 

58.     * @param key 

59.     * @param defaultObject 

60.     * @return 

61.     */  

62.    public static Object get(Context context, String key, Object defaultObject)  

63.    {  

64.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

65.                Context.MODE_PRIVATE);  

66.  

67.        if (defaultObject instanceof String)  

68.        {  

69.            return sp.getString(key, (String) defaultObject);  

70.        } else if (defaultObject instanceof Integer)  

71.        {  

72.            return sp.getInt(key, (Integer) defaultObject);  

73.        } else if (defaultObject instanceof Boolean)  

74.        {  

75.            return sp.getBoolean(key, (Boolean) defaultObject);  

76.        } else if (defaultObject instanceof Float)  

77.        {  

78.            return sp.getFloat(key, (Float) defaultObject);  

79.        } else if (defaultObject instanceof Long)  

80.        {  

81.            return sp.getLong(key, (Long) defaultObject);  

82.        }  

83.  

84.        return null;  

85.    }  

86.  

87.    /** 

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

89.     * @param context 

90.     * @param key 

91.     */  

92.    public static void remove(Context context, String key)  

93.    {  

94.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

95.                Context.MODE_PRIVATE);  

96.        SharedPreferences.Editor editor = sp.edit();  

97.        editor.remove(key);  

98.        SharedPreferencesCompat.apply(editor);  

99.    }  

100.  

101.    /** 

102.     * 清除所有数据 

103.     * @param context 

104.     */  

105.    public static void clear(Context context)  

106.    {  

107.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

108.                Context.MODE_PRIVATE);  

109.        SharedPreferences.Editor editor = sp.edit();  

110.        editor.clear();  

111.        SharedPreferencesCompat.apply(editor);  

112.    }  

113.  

114.    /** 

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

116.     * @param context 

117.     * @param key 

118.     * @return 

119.     */  

120.    public static boolean contains(Context context, String key)  

121.    {  

122.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

123.                Context.MODE_PRIVATE);  

124.        return sp.contains(key);  

125.    }  

126.  

127.    /** 

128.     * 返回所有的键值对 

129.     *  

130.     * @param context 

131.     * @return 

132.     */  

133.    public static Map

> getAll(Context context)  

134.    {  

135.        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  

136.                Context.MODE_PRIVATE);  

137.        return sp.getAll();  

138.    }  

139.  

140.    /** 

141.     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类 

142.     *  

143.     * @author zhy 

144.     *  

145.     */  

146.    private static class SharedPreferencesCompat  

147.    {  

148.        private static final Method sApplyMethod = findApplyMethod();  

149.  

150.        /** 

151.         * 反射查找apply的方法 

152.         *  

153.         * @return 

154.         */  

155.        @SuppressWarnings({ "unchecked", "rawtypes" })  

156.        private static Method findApplyMethod()  

157.        {  

158.            try  

159.            {  

160.                Class clz = SharedPreferences.Editor.class;  

161.     

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2