CommonUtils 工具类
来源:互联网 发布:mac 东芝移动硬盘 编辑:程序博客网 时间:2024/06/04 19:59
/**
* Created by Dash
*/
public class CommonUtils {
public static final String TAG = "Dash";//sp文件的xml名称
private static SharedPreferences sharedPreferences;
/**
* DashApplication.getAppContext()可以使用,但是会使用系统默认的主题样式,如果你自定义了某些样式可能不会被使用
* @param layoutId
* @return
*/
public static View inflate(int layoutId) {
View view = View.inflate(DashApplication.getAppContext(), layoutId, null);
return view;
}
/**
* dip---px
*
* @param dip 设备独立像素device independent px....1dp = 3px 1dp = 2px 1dp = 1.5px
* @return
*/
public static int dip2px(int dip) {
//获取像素密度
float density = DashApplication.getAppContext().getResources().getDisplayMetrics().density;
//
int px = (int) (dip * density + 0.5f);//100.6
return px;
}
/**
* px-dip
*
* @param px
* @return
*/
public static int px2dip(int px) {
//获取像素密度
float density = DashApplication.getAppContext().getResources().getDisplayMetrics().density;
//
int dip = (int) (px / density + 0.5f);
return dip;
}
/**
* 获取资源中的字符串
* @param stringId
* @return
*/
public static String getString(int stringId) {
return DashApplication.getAppContext().getResources().getString(stringId);
}
public static Drawable getDrawable(int did) {
return DashApplication.getAppContext().getResources().getDrawable(did);
}
public static int getDimens(int id) {
return DashApplication.getAppContext().getResources().getDimensionPixelSize(id);
}
/**
* sp存入字符串类型的值
* @param flag
* @param str
*/
public static void saveSp(String flag, String str) {
if (sharedPreferences == null) {
sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(flag, str);
edit.commit();
}
public static String getSp(String flag) {
if (sharedPreferences == null) {
sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
}
return sharedPreferences.getString(flag, "");
}
public static boolean getBoolean(String tag) {
if (sharedPreferences == null) {
sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
}
return sharedPreferences.getBoolean(tag, false);
}
public static void putBoolean(String tag, boolean content) {
if (sharedPreferences == null) {
sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean(tag, content);
edit.commit();
}
/**
* 清除sp数据
* @param tag
*/
public static void clearSp(String tag) {
if (sharedPreferences == null) {
sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.remove(tag);
edit.commit();
}
/**
* 自己写的运行在主线程的方法
* 如果是主线程,执行任务,否则使用handler发送到主线程中去执行
*
*
* @param runable
*/
public static void runOnUIThread(Runnable runable) {
//先判断当前属于子线程还是主线程
if (android.os.Process.myTid() == DashApplication.getMainThreadId()) {
runable.run();
} else {
//子线程
DashApplication.getAppHanler().post(runable);
}
}
阅读全文
0 0
- Java 工具类--CommonUtils
- CommonUtils 工具类
- CommonUtils的工具类
- 浅谈Java工具类CommonUtils的使用
- 项目实践积累我的工具类——CommonUtils
- CommonUtils
- 3-JavaScript设计模式——commonUtils工具库之接口类
- CommonUtils中的工具UUID和ToBean
- 一些帮助类:CommonUtils,,,,OkHttp3Util
- CommonUtils.toBean和CommonUtils.uuid
- 1-JavaScript设计模式——commonUtils工具库之多维数组的遍历
- 2-JavaScript设计模式——commonUtils工具库之继承函数
- commonUtils简单总结
- java小工具CommonUtils
- CommonUtils(一)
- 1、CommonUtils.uuid()和CommonUtils.toBean(map, Person.class)
- 普通的封装类CommonUtils 加载布局 ,获取像素,获取资源中的字符串,sharedPreferences存取,runOnuithreadsharedPreferences删除
- 配置一些项目中用到的小工具(一)----------CommonUtils
- 论文阅读-《Learning Deep Features for Discriminative Localization》
- NIO相关基础篇二
- GDB使用方法小记
- upload
- 紧凑存储的杜利特尔分解法Doolittle(LU分解法)_解线性方程组的直接解法
- CommonUtils 工具类
- 购物车
- Codeforces 899
- FreeBASIC学习笔记——FireFly常用控件之窗体(Form)
- 克洛特分解法Crout_解线性方程组的直接解法
- 购物车1
- 紧凑存储的克洛特分解法Crout_解线性方程组的直接解法
- 红条款信用证Red Clause L/C
- bzoj3196 二逼平衡树【线段树套Splay】