package utils; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.view.View; /** * 訂單用 */ public class CommonUtils { public static final String TAG = "logins";//sp文件的xml名稱 private static SharedPreferences sharedPreferences; /** * DashApplication.getAppContext()可以使用,但是會使用系統默認的主題樣式,如果你自定義了某些樣式可能不會被使用 * @param layoutId * @return */ public static View inflate(int layoutId) { View view = View.inflate(MyApplication.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 = MyApplication.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 = MyApplication.getAppContext().getResources().getDisplayMetrics().density; // int dip = (int) (px / density + 0.5f); return dip; } /** * 獲取資源中的字符串 * @param stringId * @return */ public static String getString(int stringId) { return MyApplication.getAppContext().getResources().getString(stringId); } public static Drawable getDrawable(int did) { return MyApplication.getAppContext().getResources().getDrawable(did); } public static int getDimens(int id) { return MyApplication.getAppContext().getResources().getDimensionPixelSize(id); } /** * sp存入字符串類型的值 * @param flag * @param str */ public static void saveString(String flag, String str) { if (sharedPreferences == null) { sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE); } SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString(flag, str); edit.commit(); } public static String getString(String flag) { if (sharedPreferences == null) { sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE); } return sharedPreferences.getString(flag, ""); } public static boolean getBoolean(String tag) { if (sharedPreferences == null) { sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE); } return sharedPreferences.getBoolean(tag, false); } public static void putBoolean(String tag, boolean content) { if (sharedPreferences == null) { sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.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 = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.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() == MyApplication.getMainThreadId()) { runable.run(); } else { //子線程 MyApplication.getAppHanler().post(runable); } } }
一個封裝好的SharedPreferences,可以拿來直接用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.