开发者社区 问答 正文

关于android SharedPreference的问题

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

展开
收起
爵霸 2016-03-18 14:23:52 1652 分享 版权
1 条回答
写回答
取消 提交回答
  • 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
    }
    
    
    2019-07-17 19:06:37
    赞同 展开评论
问答分类:
问答标签:
问答地址: