В моем новом приложении счетчик увеличивается при нажатии кнопки. Я хочу сохранить рекордер с sharedPreferences, поэтому оценка будет сохранена и показана при следующем запуске приложения. Проблема в том, что я действительно не работаю, даже с другими ответами на вопросы.android - сохранение int с sharedPreferences
package com.example.test;
public class MainActivity extends ActionBarActivity {
public int score = 0;
public int highscore = 0;
TextView tvscore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvhighscore= (TextView) findViewById(R.id.highscore);
tvscore = (TextView) findViewById(R.id.score);
Button count = (Button) findViewById(R.id.button1);
tvhighscore.setText(String.valueOf(highscore));
SharedPreferences prefs = this.getSharedPreferences("score", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("score", 0);
editor.commit();
}
public void onClick (View view) {
score++;
tvscore.setText(String.valueOf(score));
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int highscore = prefs.getInt("score", 0);
}
}
необходимо сохранить счет в 'SharedPreferences' при нажатии кнопки –
@ ρяσѕρєя K является правильным. Кроме того, убедитесь, что вы используете одно и то же имя для файла общих настроек. –
Я не использую одноименное имя '' score''? –