0

我正在开发应用程序,其中有两个搜索栏,并且我想使用共享首选项存储他们的进度,以便每当用户打开该应用程序时,他都应该得到他所做的最后更改。我已经保存了 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();

    }

}
4

3 回答 3

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 和 Seekbar,这是无法阅读的。

于 2013-04-24T07:48:02.197 回答
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) {

}
});
于 2013-04-24T07:40:58.373 回答
0

你有一个非常糟糕的方式来命名你的变量。与其调用它们seekbarand Seekbar,不如调用它们seekbar1and seekbar2。但是,保持变量名...您的问题是您使用的是相同的侦听器-因此onStopTrackingTouch将为两个 SeekBars 调用相同的方法。只是添加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();
    }
}

我不批准您使用变量名,也不评论您的代码结构 - 只是指出您遇到问题的原因。

于 2013-04-24T07:40:58.717 回答