我正在开发应用程序,我有两个搜索栏和我想使用共享偏好来存储他们的进度,以便每当用户打开该应用程序时,他应该获得他做出的最后更改。我已经完成了保存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();
}
}
答案 0 :(得分:0)
public void onStopTrackingTouch1(SeekBar Seekbar) {
newProgressValue1 = Seekbar.getProgress();
currentProgress1 = newProgressValue1 ;
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putInt(Key_PROGRESS1, newProgressValue1);
editor.commit();
}
永远不会调用此方法。这样您只能获得一个Seekbar
你应该注册SeekBar.OnSeekBarChangeListener
的两个不同的实现,一个用于第一个SeekBar
,一个用于后者。你可以通过anonmyus内部课程来做:
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
currentProgress = newProgressValue ;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(Key_PROGRESS, newProgressValue);
editor.commit();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
Seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
newProgressValue1 = Seekbar.getProgress();
currentProgress1 = newProgressValue1 ;
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putInt(Key_PROGRESS1, newProgressValue1);
editor.commit();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
答案 1 :(得分:0)
你有一个非常糟糕的命名变量的方法。而不是称他们为seekbar
和Seekbar
,你最好不要再称他们为seekbar1
和seekbar2
。但是,保持您的变量名称...您的问题是您正在使用相同的侦听器 - 因此将为两个SeekBars调用相同的方法onStopTrackingTouch
。只需添加onStopTrackingTouch1
即可。相反,您需要检查两者中的哪一个调用该方法并相应地处理它;像这样的东西:
public void onStopTrackingTouch(SeekBar seek) {
if(seek == seekbar) {
newProgressValue = seekbar.getProgress();
currentProgress = newProgressValue;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(Key_PROGRESS, newProgressValue);
editor.commit();
}
else if(seek == Seekbar) {
newProgressValue1 = Seekbar.getProgress();
currentProgress1 = newProgressValue1 ;
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putInt(Key_PROGRESS1, newProgressValue1);
editor.commit();
}
}
我不赞成您使用变量名称,也不批评您的代码结构 - 只是指出您遇到问题的原因。
答案 2 :(得分:0)
问题是你在两个搜索栏中使用相同的监听器而没有使用一个要使用的监听器,这里是正确的代码:
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) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getID() == R.id.seekbar) {
newProgressValue = seekBar.getProgress();
currentProgress = newProgressValue;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt( Key_PROGRESS, newProgressValue );
editor.commit();
} else {
newProgressValue1 = seekBar.getProgress();
currentProgress1 = newProgressValue;
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putInt( Key_PROGRESS1, newProgressValue1 );
editor.commit();
}
}
}
这应该做你想要的,但实际上你应该看看在Android和java中开发时使用的正确模式。
例如,您正在使用搜索栏和Seekbar,这是不可能阅读的。