SharedPreferencesUtil

本文提供了一个关于SharedPreferences在Android应用中如何存储和读取基本类型数据的实用工具类示例。包括整型、浮点型、布尔型、长整型及字符串类型的存取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *配置存储
 */
public class SharedPreferencesUtil {

    private final static String NAME = "settings";

    /**
     * put int value
     * @param context
     * @param key
     * @param value
     */
    public static void putIntValue(Context context,String key,int value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    /**
     * put float value
     * @param context
     * @param key
     * @param value
     */
    public static void putFloatValue(Context context,String key,float value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putFloat(key, value);
        edit.commit();
    }

    /**
     * put boolean value
     * @param context
     * @param key
     * @param value
     */
    public static void putBooleanValue(Context context,String key,boolean value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * put long value
     * @param context
     * @param key
     * @param value
     */
    public static void putLongValue(Context context,String key,long value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putLong(key, value);
        edit.commit();
    }

    /**
     * put String value
     * @param context
     * @param key
     * @param value
     */
    public static void setStringValue(Context context,String key,String value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static int getIntValue(Context context,String key,int defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, defValue);
    }

    public static float getFloatValue(Context context,String key,float defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getFloat(key, defValue);
    }

    public static long getLongValue(Context context,String key,long defValue){
         SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
         return sharedPreferences.getLong(key, defValue);
    }

    public static String getStringValue(Context context,String key,String defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, defValue);
    }

    public static boolean getBooleanValue(Context context,String key,boolean defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defValue);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值