SharedPreferences 有没有办法根据vlaue获取对应的key值,除了循环SharePreferences里面的内容。
4条回答 默认 最新
- tony4geek 2016-01-06 02:14关注
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 }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报