问题描述
我有一个应用程序,它将MainActivity中明星的总收入显示为textview。如果应用程序第一次运行,我想做的是输入0。然后,当我完成一个级别并获得一些星级(如currentScore + 50 = newScore)后,它将使用newScore textView重定向到MainActivity。
I have an app which shows the total earnings of stars in the MainActivity as textview. I want to do is input 0 if the app is run for the first time. Then afterwards as I finish a level and earn some stars (like currentScore + 50 = newScore) it will be redirected to MainActivity with the newScore textView.
这是我的MainActivity,其中textview显示分数
This is my MainActivity where the the textview shows the score
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然后更新
And for the activity where I read the current score then add 50 to it then update
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中阅读吗?
And also can a SharedPreferences be use in different activities? Like I just save a score from ActivityOne and can be read in ActivityTwo?
非常感谢!
推荐答案
好吧,你应该先阅读,然后按照我的步骤使用 SharedPreferences
Well, you should first read the SharedPreferences documentation
, and then follow my steps to save values with 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()
:
And everytime you want to keep the users Stars
you'll have to use the 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();
这篇关于如何将分数保存到SharedPreferences然后更新它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!