单例模式的ToastUtil
/**
* Created by oblivion on 2016/10/26.
*/
public class ToastUtils {
private static Toast toast;
public static void setToast(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
toast.setText(msg);
toast.show();
}
}
android中的储存工具SharedPreferences储存—-
啦啦啦~~~
package com.oblivion.zhbj.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* 专门访问和设置SharePreference的工具类, 保存和配置一些设置信息
*
* @author oblivion
*/
public class PrefUtils {
private static final String SHARE_PREFS_NAME = "oblivion";
public static void putBoolean(Context ctx, String key, boolean value) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
pref.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(Context ctx, String key,
boolean defaultValue) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
return pref.getBoolean(key, defaultValue);
}
public static void putString(Context ctx, String key, String value) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
pref.edit().putString(key, value).commit();
}
public static String getString(Context ctx, String key, String defaultValue) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
return pref.getString(key, defaultValue);
}
public static void putInt(Context ctx, String key, int value) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
pref.edit().putInt(key, value).commit();
}
public static int getInt(Context ctx, String key, int defaultValue) {
SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
Context.MODE_PRIVATE);
return pref.getInt(key, defaultValue);
}
}