如何在Android的共享首选项中保存Seekbar进度?

时间:2018-09-27 11:41:29

标签: android

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 Seconds = "seconds";
private String PREFERENCE_PROGRESS1 = "preference_progress1";

oncreate methd()
{
   sharedPreferences = getSharedPreferences(PREFERENCE_PROGRESS , Context.MODE_PRIVATE);
   currentProgress = sharedPreferences.getInt(Key_PROGRESS, 0);
}  


seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        newProgressValue = seekBar.getProgress();
        currentProgress = newProgressValue ;
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(Key_PROGRESS, newProgressValue);
        editor.commit();

4 个答案:

答案 0 :(得分:0)

取决于您要实现的目标,但是我假设这是某种设置活动,因此建议您将值保存在onStop方法中。这是您应该如何做:

[COUNT] OF [apples >= 18 OF lemon < 12] AS [bar AS qux]

如果这不是您想要的解决方案,请阐明您的问题。

答案 1 :(得分:0)

这是正确的答案,我一次要保存三个搜索栏值,

公共类MainActivity扩展Activity实现OnSeekBarChangeListener {

public TextView progress1;

private SeekBar bar1;


private int savedProgress1;

private int loadedProgress1;

private String Key_PROGRESS_1 = "bar1_progress";


SharedPreferences mPreferences;
SharedPreferences.Editor editor;



/*********************
 *
 * ONCREATE
 *
 ********************/


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.configuracion2);



    bar1 = (SeekBar)findViewById(R.id.seekbar); //make seekbar object
    bar1.setOnSeekBarChangeListener(this);   //set seekbar listener
    progress1 = (TextView)findViewById(R.id.summary);



    mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    editor = mPreferences.edit();



}


public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    Log.v("", "" + bar);
    switch (bar.getId()) {
        case R.id.fast_bar:
            progress1.setText(progress + "%");
            break;
        case R.id.climb_bar:
            progress2.setText(progress + "%");
            break;
        case R.id.platform_bar:
            progress3.setText(progress + "%");
            break;
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    seekBar.setSecondaryProgress(seekBar.getProgress());
}




protected void onResume() {
    super.onResume();

    loadedProgress1 = mPreferences.getInt("Key_PROGRESS_1", 0);
    bar1.setProgress(loadedProgress1);

    loadedProgress2 = mPreferences.getInt("Key_PROGRESS_2", 0);
    bar2.setProgress(loadedProgress2);

    loadedProgress2 = mPreferences.getInt("Key_PROGRESS_3", 0);
    bar3.setProgress(loadedProgress3);

}




protected void onPause(/*SeekBar seekBar*/) {
    super.onPause();

    savedProgress1 = bar1.getProgress();
    editor.putInt(Key_PROGRESS_1, savedProgress1);
    savedProgress2 = bar2.getProgress();
    editor.putInt(Key_PROGRESS_2, savedProgress2);
    savedProgress3 = bar3.getProgress();
    editor.putInt(Key_PROGRESS_3, savedProgress3);
    editor.commit();
}

}}

答案 2 :(得分:0)

      mySeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

//                progress = progress / 45;
//                progress = progress * 45;
                Log.i(TAG, "OnSeekBarChangeListener():progress " + progress);
                mProgress = progress;
            }

 @Override
    protected void onPause() {
        super.onPause();
        SharedPreferenceHelper.setSharedPreferenceInt(configActivity.this, SharedPreferenceHelper.PREF_FORTUDE_WALPHABKGND, mProgress);
    }

答案 3 :(得分:0)

我认为您的代码仅在您的onCreate中丢失seekBar.setProgress(currentProgress)