关于android SharedPreference的问题,在线等!谢谢!!!

SharedPreferences 有没有办法根据vlaue获取对应的key值,除了循环SharePreferences里面的内容。

You should note that while keys are guaranteed to be unique in SharedPreferences, there's no guarantee that the values will be unique. As such, this function will only return the key for the first matching value.

 String findKey(SharedPreferences sharedPreferences, String value) {
    for (Map.Entry<String, ?> entry: sharedPreferences.getAll()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }
    }
    return null; // not found
}

只能循环判断了。map

存的时候反着也存一遍,value当key,key当value

You should note that while keys are guaranteed to be unique in SharedPreferences, there's no guarantee that the values will be unique. As such, this function will only return the key for the first matching value.

String findKey(SharedPreferences sharedPreferences, String value) {
for (Map.Entry entry: sharedPreferences.getAll()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null; // not found
}