SharedUtil

                                                                                                        SharedUtil工具类

布局:

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

SharedUtil工具类:

public class SharedUtil {
    public static SharedPreferences sp;
    public static String name = "shared_demo";

    // 存放布尔类型;
    public static void putData(Context context, String key, boolean value) {
        // 获取SharedPreferences 的实例;需要用到context ;
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    // 存放object类型;
    public static void putData(Context context, String key, Object value) {
        // 获取SharedPreferences 的实例;需要用到context ;
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        }
        editor.commit();
    }

    // 根据key获取SharedPreferences里的value;通过判断defaultValue的类型,来选择不同的get方法;
    public static Object getData(Context context, String key, Object defValue) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        if (defValue instanceof String) {
            return sp.getString(key, (String) defValue);
        } else if (defValue instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defValue);
        } else if (defValue instanceof Integer) {
            return sp.getInt(key, (Integer) defValue);
        } else if (defValue instanceof Float) {
            return sp.getFloat(key, (Float) defValue);
        } else if (defValue instanceof Long) {
            return sp.getLong(key, (Long) defValue);
        }
        return null;
    }

    // 根据key来移除数据
    public static void removeData(Context context, String key) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    // 清除所有数据
    public static void clearData(Context context) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    // 根据key判断这个value是否存在;
    public static boolean isExist(Context context, String key) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sp.contains(key);
    }
}
Activity主类:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedUtil.putData(MainActivity.this, "one", true);
        SharedUtil.putData(MainActivity.this, "two", "xiaoming");
        SharedUtil.putData(MainActivity.this, "three", 123);
        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String value = (String) SharedUtil.getData(MainActivity.this,
                        "two", "two");
                Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT)
                        .show();
                SharedUtil.removeData(MainActivity.this, "two");
            }
        });
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值