2016-01-28 24 views
1

我有一个应用程序,它将MainActivity中的星星总收入显示为textview。我想要做的是输入0,如果该应用程序是第一次运行。然后,当我完成一个关卡并获得一些星星(如currentScore + 50 = newScore)后,它将通过newScore textView重定向到MainActivity。如何将分数保存到SharedPreferences然后更新它?

这是我的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中读取?

非常感谢!

+1

我认为一个改进就是让'sharedStars'和'editor'成为类的成员,所以每次用户单击按钮时都不会实例化它们。你也可以保留星星的数量,这样你就不会一遍又一遍地读它们了。 –

+1

是的,SharedPreferences可以在不同的活动之间使用。如果您使用“MODE_WORLD_READABLE”或“MODE_WORLD_WRITEABLE”*创建共享首选项文件,它们甚至可以在不同的应用程序*之间使用。 – Voicu

回答

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(); 
+0

在玩游戏后它仍然显示为零。我用你的代码编辑了我的代码。 – kcNeko

+1

@kcNeko你把它弄错了....你必须把代码放在出现'编辑器'的地方,当你想要保存值时,不要紧,如果你把它放在不同的类上,只要你在'currentScore + 50 = newScore'添加我的答案的第二个代码。 –

+0

对不起,但你可以给我看吗?我只是无法做到。 – kcNeko

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可整型,布尔型,长或字符串

和检索:

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()来添加任何要添加到应用程序中的功能。

我希望对某人有用。

+1

哦,很好的类@Perroloco,不知道那:) –

相关问题