-5

我想保存我最好的,只有一个高分存档,然后如果游戏开始在菜单屏幕上显示它。

像这样:

最佳:积分。

我有我的积分,直到你死为止,但我不知道如何保存它们。我听说过共享首选项。但是有人可以给我举个例子,最好的方法。我有代码检查你的分数是否不如最好的高分,但不知道如何正确保存它们。任何建议或帮助表示赞赏!

4

3 回答 3

1

这是一篇关于不同存储选项的文章:http: //developer.android.com/guide/topics/data/data-storage.html它还详细描述了 SharedPreferences。

于 2013-02-19T21:44:18.450 回答
1

这是一个非常快速的示例,假设您要从Activity.

要将您的值存储在首选项中:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("best_score", numberOfPoints);
editor.commit();

要加载值:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
if (sharedPreferences.contains("best_score")) {
    // we have a high score saved, load it...
    int numberOfPoints = sharedPreferences.getInt("best_score", -1);
    // here you'd like to do something with the value, for example display it.
} else {
    // there is no high score value - you should probably hide the "best score" TextView
}

"best_score"只是您告诉 Android 存储该值的一个键 - 它可以是任何东西,但重要的是每次您想要访问/操作相同的值时该键都相同 - 在您的情况下为“最佳分数”。

于 2013-02-19T21:51:58.217 回答
0

理想情况下,您应该将高分存储在 SQLite DB 中。但是 SharedPrefs 选项也是可行的。

于 2013-02-19T22:20:53.023 回答