Android 使用SharedReference保存高分
我正在制作一个游戏,我想保存一个用户的最高分数,所有的都在我的游戏中工作,但问题是,当我退出游戏时,用户的最高分数会自动重新设置,但我想让它显示给所有用户,直到它中断,然后它会更新 退出游戏后显示的对话框:Android 使用SharedReference保存高分,android,Android,我正在制作一个游戏,我想保存一个用户的最高分数,所有的都在我的游戏中工作,但问题是,当我退出游戏时,用户的最高分数会自动重新设置,但我想让它显示给所有用户,直到它中断,然后它会更新 退出游戏后显示的对话框: private void showGameOverDialog() { int previousScore=getHighScore(); if(playerScore>previousScore){
private void showGameOverDialog() {
int previousScore=getHighScore();
if(playerScore>previousScore){
saveHighScore();
}
//AlertDialog
//AlertDialog.Builder
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Exit Game");
builder.setMessage("Afraid?");
//Positive, Negative, Neutral
//Anonymous Inner Type Interface Implementation
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
exitGame();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resetGame();
}
}
);
AlertDialog exitDialog=builder.create();
exitDialog.show();
}
获得高分的方法
private int getHighScore(){
SharedPreferences highScore=getSharedPreferences("high",0);
return highScore.getInt("highScore",0);
}
private void saveHighScore(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0);
SharedPreferences.Editor writer=sharedPreferences.edit();
int displayValue=playerScore;
//Username-Highscore
writer.putInt("display",displayValue);
writer.commit();
display();
}
private void display(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0 );
String userString=sharedPreferences.getString("playerName","Nothing found");
int highScore=sharedPreferences.getInt("display",0);
username.setText(userString+"-"+highScore);
}
保存高分
private int getHighScore(){
SharedPreferences highScore=getSharedPreferences("high",0);
return highScore.getInt("highScore",0);
}
private void saveHighScore(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0);
SharedPreferences.Editor writer=sharedPreferences.edit();
int displayValue=playerScore;
//Username-Highscore
writer.putInt("display",displayValue);
writer.commit();
display();
}
private void display(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0 );
String userString=sharedPreferences.getString("playerName","Nothing found");
int highScore=sharedPreferences.getInt("display",0);
username.setText(userString+"-"+highScore);
}
高分
private int getHighScore(){
SharedPreferences highScore=getSharedPreferences("high",0);
return highScore.getInt("highScore",0);
}
private void saveHighScore(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0);
SharedPreferences.Editor writer=sharedPreferences.edit();
int displayValue=playerScore;
//Username-Highscore
writer.putInt("display",displayValue);
writer.commit();
display();
}
private void display(){
SharedPreferences sharedPreferences=getSharedPreferences("data",0 );
String userString=sharedPreferences.getString("playerName","Nothing found");
int highScore=sharedPreferences.getInt("display",0);
username.setText(userString+"-"+highScore);
}
用此替换getHighScore()方法,然后重试。。这可能有用
private int getHighScore(){
SharedPreferences highScore=getSharedPreferences("data",0);
return highScore.getInt("display",0);
}
试试这个
保存高分:
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("myHighScore", --your high score--);
editor.commit();
展示
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int myInt = preferences.getInt("myHighScore", -1);
username.setText("High Score : " + myInt);
除去
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.remove("myHighScore");
editor.commit();
首先,你要在你的应用程序上创建两个共享首选项位置,一个叫做“高”,第二个叫做“数据”,然后你要将高分值保存在一个位置,并尝试从另一个位置获取它。我希望你现在明白了问题所在。 这是一个更好的解决方案示例
private static final String HIGH_PREF_KEY = "high_score";
SharedPreferences sharedPreferences=getSharedPreferences("GAME_SHAREDPREF",0);
puplic void saveHighScore(int score){
SharedPreferences.Editor writer = sharedPreferences.edit();
writer.putInt(HIGH_PREF_KEY,score).commit();
}
puplic int getHighScore(){
return sharedPreferences.getInt(HIGH_PREF_KEY, 0);
}
创建具有名称的共享首选项,即“数据”。用“highScore”键设置高分,然后输入你的分数。当您检索时,获得“highScore”并更新 您可以使用,一个用于android共享首选项的包装工具
如果使用此库,请在应用程序中创建共享首选项
Preference.load().using(this).with(PREFERENCE_NAME).prepare();
只需一行即可保存分数
private void saveHighScore(){
Preference.putInt("highScore", highScore);
}
得分
private int getHighScore(){
return Preference.getInt("highScore");
}
并使用
private void resetScore() {
Preference.remove("highScore);
}
显示分数
private void display(){
int highScore = getHighScore();
username.setText(userString + "-" + highScore);
}
@VishalSharma首选项文件的名称是“data”,您在其中保存高分,因此在检索名称时,应仅使用“data”而不是“high”,请尝试上述解决方案,让我知道这是否有效。@VishalSharma请立即尝试。。。你用来保存和检索高分的关键是不同的,但高分和用户名在我们创建新高分时会发生变化,否则它会显示默认文本视图。我想每次都显示高分。因此,请告诉我如何保存、更新或从共享首选项中删除高分