gpt4 book ai didi

java - 需要为安卓游戏保存高分

转载 作者:IT老高 更新时间:2023-10-28 23:38:26 27 4
gpt4 key购买 nike

这很简单,我需要做的就是为游戏保存一个高分(整数)。我假设最简单的方法是将其存储在文本文件中,但我真的不知道如何去做。

最佳答案

如果您只需要存储一个整数,那么 SharedPreferences最适合您使用:

//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();

获得偏好:

//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value

当然,将 "key" 替换为您的高分值的键,将 "myPrefsKey" 替换为您的偏好的键(这些可以是任何值。这很好将它们设置为可识别且独特的东西)。

关于java - 需要为安卓游戏保存高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8407286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com