Java 需要为Android游戏保存高分吗

Java 需要为Android游戏保存高分吗,java,android,Java,Android,这很简单,我只需要为游戏保存一个高分(整数)。我假设最简单的方法是将其存储在文本文件中,但我真的不知道如何执行此操作。如果您只需要存储一个整数,则最好使用: //setting preferences SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); Editor editor = prefs.edit(); editor.putInt("key", score); e

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

如果您只需要存储一个整数,则最好使用:

//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”
,用首选项的键替换
“MyPrefsky”
(可以是任何类型的。最好将它们设置为可识别和唯一的键)。

使用:

这是储存此类物品最简单的方法。

我想这会对您有所帮助:

The SharedPreferences class provides a general framework that allows you to save and
retrieve persistent key-value pairs of primitive data types. You can use
SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 
This data will persist across user sessions (even if your application is killed).

User Preferences

Shared preferences are not strictly for saving "user preferences," such as what ringtone
a user has chosen. If you're interested in creating user preferences for your
application, see PreferenceActivity, which provides an Activity framework for you to 
create user preferences, which will be automatically persisted (using shared preferences).

To get a SharedPreferences object for your application, use one of two methods:

    getSharedPreferences() - Use this if you need multiple preferences files identified
by name, which you specify with the first parameter.
    getPreferences() - Use this if you need only one preferences file for your Activity.
Because this will be the only preferences file for your Activity, you don't supply a name.

To write values:

    Call edit() to get a SharedPreferences.Editor.
    Add values with methods such as putBoolean() and putString().
    Commit the new values with commit()

To read values, use SharedPreferences methods such as getBoolean() and getString().

如我所见,保存高分的最佳方法是共享参考资料。

只是确认一下,这是否会存储数据,以便下次我运行游戏时可以获取它?是的,此数据在应用程序运行之间持续存在。只需确保调用commit();在编辑器上保存它们!非常感谢,我现在将尝试“MyPrefsky”必须是唯一的,仅适用于我的应用程序或全局?因此,我将“MyPrefsky”命名为“SampleGameIMade”,将“key”命名为“highScore”,对吗?需要添加几行文本来解释此代码块的真正功能。
public class HighScores extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high);

        //get text view
        TextView scoreView = (TextView)findViewById(R.id.high_scores_list);
        //get shared prefs
        SharedPreferences scorePrefs = getSharedPreferences(PlayGame.GAME_PREFS, 0);
        //get scores
        String[] savedScores = scorePrefs.getString("highScores", "").split("\\|");
        //build string
        StringBuilder scoreBuild = new StringBuilder("");
        for(String score : savedScores){
            scoreBuild.append(score+"\n");
        }
        //display scores
        scoreView.setText(scoreBuild.toString());
    }

}
public class HighScores extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high);

        //get text view
        TextView scoreView = (TextView)findViewById(R.id.high_scores_list);
        //get shared prefs
        SharedPreferences scorePrefs = getSharedPreferences(PlayGame.GAME_PREFS, 0);
        //get scores
        String[] savedScores = scorePrefs.getString("highScores", "").split("\\|");
        //build string
        StringBuilder scoreBuild = new StringBuilder("");
        for(String score : savedScores){
            scoreBuild.append(score+"\n");
        }
        //display scores
        scoreView.setText(scoreBuild.toString());
    }

}