使用sharedPreferences的onPause和onResume方法

时间:2015-03-19 02:34:35

标签: java android sharedpreferences onresume onpause

所以我试图将我的SeekBar值保存在onPause上,然后让它们重新出现在onResume上。我的代码有效,但由于某种原因,只保存列表中的第一个。如果我更改红色SeekBar然后保存但没有别的,如果我将红色保持为0并将绿色移至40然后绿色将保存但是如果我改变红色,则只有红色将保存。我不知道为什么会这样。我必须使用onResume或onPause,否则我会使我的变量变为静态。

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.graphics.Color;



public class ColorChooserActivity extends ActionBarActivity
implements OnSeekBarChangeListener {

    // define variables for the widgets
    private SeekBar redSeekBar;
    private SeekBar greenSeekBar;
    private SeekBar blueSeekBar;
    private SeekBar alphaSeekBar;
    private View colorView;
    private TextView colorTextView;

    //instance variable
    private int colorValue = 0;
    private int redValue = 0;
    private int greenValue = 0;
    private int blueValue = 0;
    private int alphaValue = 0;
    private String output;

    // define the SharedPreferences object
    private SharedPreferences savedValues;

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

        // get references to the widgets
        redSeekBar = (SeekBar) findViewById(R.id.redSeekBar);
        greenSeekBar = (SeekBar) findViewById(R.id.greenSeekBar);
        blueSeekBar = (SeekBar) findViewById(R.id.blueSeekBar);
        alphaSeekBar = (SeekBar) findViewById(R.id.alphaSeekBar);
        colorView = (View) findViewById(R.id.colorView);
        colorTextView = (TextView) findViewById(R.id.colorTextView);

        // add listeners
        redSeekBar.setOnSeekBarChangeListener(this);
        greenSeekBar.setOnSeekBarChangeListener(this);
        blueSeekBar.setOnSeekBarChangeListener(this);
        alphaSeekBar.setOnSeekBarChangeListener(this);

        // get SharedPreferences object
        savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_color_chooser, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPause() {
      // save the instance variables
        SharedPreferences.Editor editor = savedValues.edit();
        editor.putInt("redValue", redValue);
        editor.putInt("greenValue", greenValue);
        editor.putInt("blueValue", blueValue);
        editor.putInt("alphaValue", alphaValue);
        editor.putInt("colorValue", colorValue);
        editor.putString("output", output);
        editor.commit();

        super.onPause();
    }

    @Override
    public void onResume() {

        //get the instance variables
        redValue = savedValues.getInt("redValue", redValue);
        greenValue = savedValues.getInt("greenValue", greenValue);
        blueValue = savedValues.getInt("blueValue", blueValue);
        alphaValue = savedValues.getInt("alphaValue", alphaValue);
        colorValue = savedValues.getInt("colorValue", colorValue);
        output = savedValues.getString("output", output);



        // set widgets
        colorView.setBackgroundColor(colorValue);
        colorTextView.setText(output);
        redSeekBar.setProgress(redValue);
        greenSeekBar.setProgress(greenValue);
        blueSeekBar.setProgress(blueValue);
        alphaSeekBar.setProgress(alphaValue);

        super.onResume();

    }

    // argb method
    public int argb(int a, int r, int g, int b){
        return Color.argb(a, r, g, b);
    }

    //*****************************************************
    // Event handler for the SeekBar
    //*****************************************************
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
    {
        redValue = redSeekBar.getProgress();
        greenValue = greenSeekBar.getProgress();
        blueValue = blueSeekBar.getProgress();
        alphaValue = alphaSeekBar.getProgress();

        colorValue = argb(alphaValue, redValue, greenValue, blueValue);

        colorView.setBackgroundColor(colorValue);

        output = "Red: " + redValue + " Green: " + greenValue +
                " Blue: " + blueValue + " Alpha: " + alphaValue;

        colorTextView.setText(output);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar){
    }


}

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的onProgressChanged方法中。你有几个搜索栏,我建议你为它们中的每一个实现一个单独的OnSeekBarChangeListener,或者在保存它的值之前检查搜索栏的id。

基本上,您应该在onCreate方法中使用此类内容(基于SO用户对您的问题的评论):

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {


    switch (seekBar.getId()) {
    case R.id.redSeekBar:
        redValue = redSeekBar.getProgress();
        break;
    case R.id.greenSeekBar:
        greenValue = greenSeekBar.getProgress();
        break;
    case R.id.blueSeekBar:
        blueValue = blueSeekBar.getProgress();
        break;
    case R.id.alphaSeekBar:
        alphaValue = alphaSeekBar.getProgress();
        break;
    }

    colorValue = argb(alphaValue, redValue, greenValue, blueValue);

    colorView.setBackgroundColor(colorValue);

    output = "Red: " + redValue + " Green: " + greenValue + " Blue: "
            + blueValue + " Alpha: " + alphaValue;

    colorTextView.setText(output);
}

我希望这会有所帮助。