1 /** SharedPreferences简易存取。 */ 2 public class Prefs { 3 /** 4 * 向首选项中设置值。</br> 5 * 6 * value的实际类型需要是<b>boolean</b>、<b>float</b>、<b>int</b>、<b>long</b>、<b>String</b>之一,否则不作处理。 7 * */ 8 public static void put(Context context, String key, Object value) { 9 Editor editor = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit(); 10 11 if (value instanceof Boolean) { 12 editor.putBoolean(key, (Boolean) value); 13 } else if (value instanceof Float) { 14 editor.putFloat(key, (Float) value); 15 } else if (value instanceof Integer) { 16 editor.putInt(key, (Integer) value); 17 } else if (value instanceof Long) { 18 editor.putLong(key, (Long) value); 19 } else if (value instanceof String) { 20 editor.putString(key, (String) value); 21 } else { 22 // 按说应该抛异常。 23 } 24 25 editor.commit(); 26 } 27 28 /** 从首选项中取值。 */ 29 @SuppressWarnings("unchecked") 30 public static <P> P get(Context context, String key, P defValue) { 31 SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); 32 33 Object result = null; 34 35 if (defValue instanceof Boolean) { 36 result = prefs.getBoolean(key, (Boolean) defValue); 37 } else if (defValue instanceof Float) { 38 result = prefs.getFloat(key, (Float) defValue); 39 } else if (defValue instanceof Integer) { 40 result = prefs.getInt(key, (Integer) defValue); 41 } else if (defValue instanceof Long) { 42 result = prefs.getLong(key, (Long) defValue); 43 } else if (defValue instanceof String) { 44 result = prefs.getString(key, (String) defValue); 45 } 46 47 return (P) result; 48 } 49 }
应该没有什么再简化的余地了。
告别强转和一大堆方法提示,省心省力。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** Log的简易类。 */ 2 public class L { 3 /** 日志标签。*/ 4 public static final String TAG = "MTK"; 5 6 public static void v(String msg) { 7 Log.v(TAG, msg); 8 } 9 10 public static void v(boolean msg) { 11 Log.v(TAG, msg + ""); 12 } 13 14 public static void v(int msg) { 15 Log.v(TAG, msg + ""); 16 } 17 18 public static void d(String msg) { 19 Log.d(TAG, msg); 20 } 21 22 public static void d(boolean msg) { 23 Log.d(TAG, msg + ""); 24 } 25 26 public static void d(int msg) { 27 Log.d(TAG, msg + ""); 28 } 29 30 public static void i(String msg) { 31 Log.i(TAG, msg); 32 } 33 34 public static void i(boolean msg) { 35 Log.i(TAG, msg + ""); 36 } 37 38 public static void i(int msg) { 39 Log.i(TAG, msg + ""); 40 } 41 42 public static void w(String msg) { 43 Log.w(TAG, msg); 44 } 45 46 public static void w(boolean msg) { 47 Log.w(TAG, msg + ""); 48 } 49 50 public static void w(int msg) { 51 Log.w(TAG, msg + ""); 52 } 53 54 public static void e(String msg) { 55 Log.e(TAG, msg); 56 } 57 58 public static void e(boolean msg) { 59 Log.e(TAG, msg + ""); 60 } 61 62 public static void e(int msg) { 63 Log.e(TAG, msg + ""); 64 } 65 66 public static void wtf(String msg) { 67 Log.wtf(TAG, msg); 68 } 69 70 public static void wtf(boolean msg) { 71 Log.wtf(TAG, msg + ""); 72 } 73 74 public static void wtf(int msg) { 75 Log.wtf(TAG, msg + ""); 76 } 77 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 简易Toast。 */ 2 public class T { 3 /** 4 * 显示长时间的Toast。 5 * 6 * @see Toast#LENGTH_LONG 7 */ 8 public static void showLong(Context context, String text) { 9 Toast.makeText(context, text, Toast.LENGTH_LONG).show(); 10 } 11 12 /** 13 * 显示长时间的Toast。 14 * 15 * @see Toast#LENGTH_SHORT 16 */ 17 public static void showShort(Context context, String text) { 18 Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); 19 } 20 }
请随意取用。