我正在开发应用程序,其中有两个搜索栏,并且我想使用共享首选项存储他们的进度,以便每当用户打开该应用程序时,他都应该得到他所做的最后更改。我已经保存了 1 个搜索栏,但在 2 个搜索栏的情况下没有得到正确的答案。你能帮我么
public class Apartment extends Activity implements SeekBar.OnSeekBarChangeListener {
SeekBar seekBar,Seekbar;
private int newProgressValue,currentProgress;
private int newProgressValue1,currentProgress1;
private SharedPreferences sharedPreferences,sharedPreferences1 ;
private String Key_PROGRESS = "key_progress";
private String Key = "key_value";
private String PREFERENCE_PROGRESS = "preference_progress";
private String Key_PROGRESS1 = "key_progress1";
private String Key1 = "key_value1";
private String PREFERENCE_PROGRESS1 = "preference_progress1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(PREFERENCE_PROGRESS , Context.MODE_PRIVATE);
currentProgress = sharedPreferences.getInt(Key_PROGRESS, 0);
sharedPreferences1 = getSharedPreferences(PREFERENCE_PROGRESS1 , Context.MODE_PRIVATE);
currentProgress1 = sharedPreferences1.getInt(Key_PROGRESS1, 0);
setContentView(R.layout.activity_apart);
seekBar=(SeekBar)findViewById(R.id.seekbar);
Seekbar=(SeekBar)findViewById(R.id.seekbar1);
int max = 5;
seekBar.setMax(max);
Seekbar.setMax(max);
seekBar.setProgress(currentProgress);
Seekbar.setProgress(currentProgress1);
seekBar.setOnSeekBarChangeListener(this);
Seekbar.setOnSeekBarChangeListener(this);
}
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar ) {
// TODO Auto-generated method stub
newProgressValue = seekBar.getProgress();
currentProgress = newProgressValue ;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(Key_PROGRESS, newProgressValue);
editor.commit();
}
public void onStopTrackingTouch1(SeekBar Seekbar) {
newProgressValue1 = Seekbar.getProgress();
currentProgress1 = newProgressValue1 ;
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putInt(Key_PROGRESS1, newProgressValue1);
editor.commit();
}
}