如何将分数保存到SharedPreferences然后更新它?

时间:2016-01-28 18:41:05

标签: java android sharedpreferences

我有一个应用程序,它将MainActivity中明星的总收入显示为textview。如果应用程序第一次运行,我想做的是输入0。然后,当我完成一个级别并获得一些星级(如currentScore + 50 = newScore)后,它将使用newScore textView重定向到MainActivity。

这是我的MainActivity,textview显示分数

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);

  }

还可以在不同的活动中使用SharedPreferences吗?就像我只是从ActivityOne保存一个分数,可以在ActivityTwo中读取?

非常感谢!

2 个答案:

答案 0 :(得分:1)

那么,您应首先阅读the SharedPreferences documentation,然后按照我的步骤使用SharedPreferences保存值

您的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();

答案 1 :(得分:1)

我曾经写过一个有用的SharedPreferences类来轻松编写/检索数据,你可以从这里复制/粘贴:

https://github.com/asystat/bus_locator/blob/master/benasque2014/Autobus/src/com/example/com/benasque2014/mercurio/KeyStoreController.java

然后你这样写:

KeyStoreController.getKeyStore().setPreference("yourKey", yourValue)

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 Activity的onCreate方法中调用KeyStoreController.getKeyStore()。appOpened(),然后您可以将getTimesOpened()和isFirstLaunch()用于您要添加到您的任何功能应用

我希望它对某人有用。