所以我試圖保存我的SeekBar值onPause,然後讓他們重新出現onResume。我的代碼有效,但由於某種原因,只有列表中的第一個被保存。如果我改變了紅色的SeekBar,然後保存但沒有別的,如果我在0處留下紅色並將綠色移動到40,那麼綠色將會保存,但如果我改變紅色,只有紅色會保存。我不知道爲什麼會發生這種情況。我必須使用onResume或onPause,否則我會使我的變量變爲靜態的。onPause和onResume方法使用sharedPreferences
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){
}
}
您應該使用onSaveInstanceState()和onRestoreInstanceState()方法來保存狀態並恢復活動的狀態。 – 2015-03-19 04:12:03