Java 如何将分数保存到SharedReferences,然后进行更新?

Java 如何将分数保存到SharedReferences,然后进行更新?,java,android,sharedpreferences,Java,Android,Sharedpreferences,我有一个应用程序,它以文本视图的形式显示主要活动中明星的总收入。如果应用程序是第一次运行,我想做的是输入0。然后,当我完成一个关卡并获得一些星星(比如currentScore+50=newScore)时,它将通过newScore文本视图重定向到MainActivity 这是我的主要活动,文本视图显示分数 public class MainActivity extends Activity { private static TextView txt_stars; @Override

我有一个应用程序,它以文本视图的形式显示主要活动中明星的总收入。如果应用程序是第一次运行,我想做的是输入0。然后,当我完成一个关卡并获得一些星星(比如currentScore+50=newScore)时,它将通过newScore文本视图重定向到MainActivity

这是我的主要活动,文本视图显示分数

public class MainActivity extends Activity {

  private static TextView txt_stars;

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

    public void updateStars() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("Stars", 0);
    editor.commit();
    txt_stars = (TextView) findViewById(R.id.txtStars);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        //the key is "Stars" don't forget to type it as you created the Sp
    String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
    txt_stars.setText(stars);
}
}
对于我阅读当前分数的活动,再加上50分,然后更新

public void onClick(DialogInterface dialog, int whichButton) {

     SharedPreferences sharedPreferences = 
     PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     Integer newstars = sharedPreferences.getInt("Stars", 0);
     Integer totalStars = newstars + 50;
     SharedPreferences.Editor editor = sharedPreferences.edit();
     editor.putInt("Stars", totalStars);
     editor.commit();

  //please help me improve my code here

    Intent nextForm = new Intent(.MainActivity");
    startActivity(nextForm);

  }
共享的参考资料也可以在不同的活动中使用吗?就像我刚刚从ActivityOne中保存了一个分数,并且可以在ActivityII中读取一样


非常感谢

您应该先阅读,然后按照我的步骤使用
SharedReferences

您的
UpdateStars()
方法应该如下所示:

public class updateStars() {
txt_stars = (TextView) findViewById(R.id.txtStars);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//the key is "Stars" don't forget to type it as you created the Sp
String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
txt_stars.setText(stars);
}
每次你想让用户
Stars
时,你必须使用
putInt()

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Stars", YOUR_INT_VALUE); //Here you save the stars of the user
editor.commit();

我曾经编写过一个有用的SharedReferences类,可以轻松地写入/检索数据,您可以从这里复制/粘贴这些数据:

然后你这样写:

KeyStoreController.getKeyStore().setPreference("yourKey", yourValue)
您的值可以是int、boolean、long或string

以及检索:

KeyStoreController.getKeyStore().getInt("yourKey", defaultValue)
KeyStoreController.getKeyStore().getLong("yourKey", defaultValue)
KeyStoreController.getKeyStore().getString("yourKey", defaultValue)
KeyStoreController.getKeyStore().getBoolean("yourKey", defaultValue)
您还可以在MainApplication或Launcher活动的onCreate方法中调用KeyStoreController.getKeyStore().appOpened(),然后您可以使用getTimesOpened()和isFirstLaunch()为您要添加到应用程序中的任何功能


我希望它能对某些人有用。

我认为一个改进就是让
sharedStars
editor
成为类的成员,这样你就不会每次用户单击按钮时都实例化它们。您还可以保留本地的星星计数,这样您就不会一遍又一遍地阅读它们。是的,SharedReference可以在不同的活动之间使用。如果你用
MODE\u-WORLD\u-READABLE
MODE\u-WORLD\u-WRITEABLE
创建一个共享首选项文件,它们甚至可以在不同的应用程序之间使用。哦,nice class@Perroloco,我不知道:)在玩游戏后它仍然显示为零。我用你们的代码编辑了上面的代码。@kcNeko你们弄错了。。。。当你想保存值时,你必须把代码放在
编辑器出现的地方,不管你是否把它放在不同的类上,只要你在哪里添加我答案的第二个代码就行了。很抱歉,你能给我看一下吗?我就是弄不好。非常感谢。我在那里看到了我的错误,“SharedReferences=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());”应为“SharedReferences SharedReferences=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());”@kcNeko我很高兴能帮你快乐编码^^