Android的共享preferences例如高分? [英] android shared preferences example for high scores?

查看:162
本文介绍了Android的共享preferences例如高分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜 我想知道是否有实现使用共享preferences高分列表中的任何简单的例子?即有pre确定高分的开始,然后更新用户得到的分数?

Hi I was wondering is there any simple example of implementing a high score list using shared preferences? ie have pre determined high scores at the start and then update the list dependingt on the score a user gets?

推荐答案

是,你不能真正保存列表或者类似的东西。共享preferences只支持布尔,浮点数,整型,字符串,长和设置。

If you want to use shared preferences the problem is that you can't really store a list or something like that. Shared preferences only supports boolean, float, int, String, long and Set.

所以,你最好的选择是设置。在那里,您可以将您的高分的每个值转换为字符串,将其添加到组,然后存储在共享preferences这一套。

So your best choice is the Set. There you can convert each value of your highscore to a string, add it to a Set and then store this set in the shared preferences.

在应用程序启动时,你可以检索设定,转换成字符串返回整数或任何你用它来重新present高分。

During startup of your application you can retrieve the set, convert the Strings back to ints or whatever you use to represent the highscore.

例如见这个方法:

<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html#putStringSet(java.lang.String" rel="nofollow">http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html#putStringSet(java.lang.String,为java.util.Set)

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putStringSet(java.lang.String, java.util.Set)

由于MisterSqounk指出,设定只适用,因为API级别11,所以,如果你的编码下面,我建议直接存储值使用按键例如像highscore1,highscore2,...整数检索当高分值,你可以使用共享preferences#包含(字符串键)来检查值是否可以遍历所有的按键和。

As MisterSqounk pointed out, Set is only available since API Level 11. So if you're coding for below, I would suggest to store the values directly as ints using keys like for example highscore1, highscore2, ... When retrieving the highscore values you could iterate over all keys and using SharedPreferences#contains(String key) to check whether a value is available.

这篇关于Android的共享preferences例如高分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆