gpt4 book ai didi

java - 使用 sharedPreferences 的 onPause 和 onResume 方法

转载 作者:行者123 更新时间:2023-11-30 02:17:00 24 4
gpt4 key购买 nike

所以我试图在暂停时保存我的 SeekBar 值,然后让它们在恢复时重新出现。我的代码有效,但由于某种原因,只保存了列表中的第一个。如果我更改红色 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){
}


}

最佳答案

我认为问题出在您的 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);
}

希望对您有所帮助。

关于java - 使用 sharedPreferences 的 onPause 和 onResume 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com