美文网首页
Android 常用工具类

Android 常用工具类

作者: wuchao226 | 来源:发表于2018-11-02 14:06 被阅读17次

    1、界面 相关工具类UiUtils

    /**
     * @author: wuchao
     * @date: 2018/11/2 13:11
     * @desciption: 界面 相关工具类
     */
    public class UiUtils {
    
        private UiUtils() {
        }
    
        public static UiUtils getInstance() {
            return Holder.INSTANCE;
        }
    
        /**
         * dip转px
         *
         * @param dip
         * @return
         */
        public static int dip2px(float dip) {
            float scale = getResources().getDisplayMetrics().density;
            return (int) (dip * scale + 0.5f);
        }
    
        public static Resources getResources() {
            return getContext().getResources();
        }
    
        /**
         * 获取上下文
         *
         * @return
         */
        public static Context getContext() {
            return AppApplication.getContext();
        }
    
        /**
         * 屏幕宽度
         *
         * @return
         */
        public static int getScreenWidth() {
            return getResources().getDisplayMetrics().widthPixels;
        }
    
        /**
         * 屏幕高度
         *
         * @return
         */
        public static int getScreenHeight() {
            return getResources().getDisplayMetrics().heightPixels;
        }
    
        /**
         * px转dip
         *
         * @param px
         * @return
         */
        public static int px2dip(int px) {
            final float scale = getResources().getDisplayMetrics().density;
            return (int) (px / scale + 0.5f);
        }
    
        /**
         * 获取字符数组
         */
        public static String[] getStringArray(int id) {
            return getResources().getStringArray(id);
        }
    
        /**
         * 获取颜色id
         */
        public static int getColor(int colorId) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return getResources().getColor(colorId, null);
            } else {
                return getResources().getColor(colorId);
            }
        }
    
        public static Drawable getDrawable(int id) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return getResources().getDrawable(id, null);
            } else {
                return getResources().getDrawable(id);
            }
        }
    
        /**
         * 根据id获取尺寸
         */
        public static int getDimens(int id) {
            return getResources().getDimensionPixelSize(id);
        }
    
        public static View inflate(int id) {
            return View.inflate(getContext(), id, null);
        }
    
        public static String getString(int resId) {
            return getContext().getString(resId);
        }
    
        public static int getTextSize(int id) {
            TypedValue value = new TypedValue();
            getResources().getValue(id, value, true);
            return (int) TypedValue.complexToFloat(value.data);
        }
    
        /**
         * 状态栏高度
         *
         * @return
         */
        public static int getStatusBarHeight() {
            int statusBarHeight = -1;
            //获取status_bar_height资源的ID
            int resourceId = getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
            return statusBarHeight;
        }
    
        private static final class Holder {
            private static final UiUtils INSTANCE = new UiUtils();
        }
    }
    

    2、SP 工具类

    /**
     * @author: wuchao
     * @date: 2018/11/2 13:44
     * @desciption: SP 工具类
     */
    public class SPUtils {
        /**
         * 提示:
         * Activity.getPreferences(int model)生成 Activity名.xml  用于Activity内部存储
         * PreferenceManager.getDefaultSharedPreferences(Context)生成 包名_preferences.xml
         * Context.getSharedPreferences(String name,int model)生成name.xml
         */
        private static SharedPreferences mPreferences =
                UiUtils.getContext().getSharedPreferences(Constant.TABLE_PREFS, Context.MODE_PRIVATE);
    
        private static SharedPreferences.Editor mEditor = mPreferences.edit();
    
        /**
         * 向SP存入指定key对应的数据
         * 其中value可以是String、boolean、float、int、long等各种基本类型的值
         *
         * @param key
         * @param value
         */
        public static void putString(String key, String value) {
            mEditor.putString(key, value);
            mEditor.commit();
        }
    
        public static void putBoolean(String key, boolean value) {
            mEditor.putBoolean(key, value);
            mEditor.commit();
        }
    
        public static void putFloat(String key, float value) {
            mEditor.putFloat(key, value);
            mEditor.commit();
        }
    
        public static void putInt(String key, int value) {
            mEditor.putInt(key, value);
            mEditor.commit();
        }
    
        public static void putLong(String key, long value) {
            mEditor.putLong(key, value);
            mEditor.commit();
        }
    
        /**
         * 清空SP里所以数据
         */
        public static void clear() {
            mEditor.clear();
            mEditor.commit();
        }
    
        /**
         * 删除SP里指定key对应的数据项
         *
         * @param key
         */
        public static void remove(String key) {
            mEditor.remove(key);
            mEditor.commit();
        }
    
        /**
         * 获取SP数据里指定key对应的value。如果key不存在,则返回默认值defValue。
         *
         * @param key
         * @param defValue
         * @return
         */
        public static String getString(String key, String defValue) {
            return mPreferences.getString(key, defValue);
        }
    
        public static boolean getBoolean(String key, boolean defValue) {
            return mPreferences.getBoolean(key, defValue);
        }
    
        public static float getFloat(String key, float defValue) {
            return mPreferences.getFloat(key, defValue);
        }
    
        public static int getInt(String key, int defValue) {
            return mPreferences.getInt(key, defValue);
        }
    
        public static long getLong(String key, long defValue) {
            return mPreferences.getLong(key, defValue);
        }
    
        /**
         * 判断SP是否包含特定key的数据
         *
         * @param key
         * @return
         */
        public static boolean contains(String key) {
            return mPreferences.contains(key);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android 常用工具类

          本文链接:https://www.haomeiwen.com/subject/tmtcxqtx.html