Android:数据存储问题(SharedPreferences)
Android: issue with data storage (SharedPreferences)
我正在学习 android,对于这个项目,我需要保存用户数据 - 按钮的颜色变化,在本例中 -。在程序期间发生更改(onClick),但是当我重新启动应用程序时,没有任何反应 - 更改尚未保存(或读取...)有人可以帮助我吗?代码:
final String paintKey = "paint";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCreate();
preferences();
togglePlay();
}
public void preferences(){ //the issue in this method?
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
data = settings.getString("stage", "Indoors");
settings.getBoolean(paintKey,false);
String backGround = settings.getString("stage", "Indoors");
if (backGround.equals("Indoors")) {
Picasso.with(this).load(R.drawable.shocked_crowd).fit().centerCrop().into(stage);
}
if (backGround.equals("Street")) {
Picasso.with(this).load(R.drawable.coins).fit().centerCrop().into(stage);
}
}
public void changeColor(){
if(!paint) { //paint variable has global scope and it is set to false
c1.setBackgroundColor(Color.YELLOW);
paint = true;
}else{
c1.setBackgroundColor(Color.BLUE);
paint = false;
}
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("paint", paint);
editor.commit();
}
编辑:onClick 方法:
public void onClick(View v) {
if(v==color){
changeColor();
}
编辑:我现在是这样的:
public void preferences(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
data = settings.getString("stage", "Indoors");
final String paintKey = "paint";
settings.getBoolean(paintKey,false);
错了吗?如果我把编辑器而不是设置我得到红色下划线
为了使用 SharedPreferences
,您需要一个全局密钥
final String paintKey = "paint"
要写入布尔值信息 SharedPreferences
使用
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean(paintKey, paint).commit();
稍后阅读该数据
paint = settings.getBoolean(paintKey, false);
settings.getBoolean(paintKey,false);
此行从 SharedPreferences 获取一个值并立即忽略它。您必须将 return 值保存在变量中以便稍后使用它:
boolean paint = settings.getBoolean(paintKey,false);
这将创建一个只能在同一方法中使用的局部变量。如果您需要在其他方法中使用该值,请改为创建一个字段。
我正在学习 android,对于这个项目,我需要保存用户数据 - 按钮的颜色变化,在本例中 -。在程序期间发生更改(onClick),但是当我重新启动应用程序时,没有任何反应 - 更改尚未保存(或读取...)有人可以帮助我吗?代码:
final String paintKey = "paint";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCreate();
preferences();
togglePlay();
}
public void preferences(){ //the issue in this method?
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
data = settings.getString("stage", "Indoors");
settings.getBoolean(paintKey,false);
String backGround = settings.getString("stage", "Indoors");
if (backGround.equals("Indoors")) {
Picasso.with(this).load(R.drawable.shocked_crowd).fit().centerCrop().into(stage);
}
if (backGround.equals("Street")) {
Picasso.with(this).load(R.drawable.coins).fit().centerCrop().into(stage);
}
}
public void changeColor(){
if(!paint) { //paint variable has global scope and it is set to false
c1.setBackgroundColor(Color.YELLOW);
paint = true;
}else{
c1.setBackgroundColor(Color.BLUE);
paint = false;
}
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("paint", paint);
editor.commit();
}
编辑:onClick 方法:
public void onClick(View v) {
if(v==color){
changeColor();
}
编辑:我现在是这样的:
public void preferences(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
data = settings.getString("stage", "Indoors");
final String paintKey = "paint";
settings.getBoolean(paintKey,false);
错了吗?如果我把编辑器而不是设置我得到红色下划线
为了使用 SharedPreferences
,您需要一个全局密钥
final String paintKey = "paint"
要写入布尔值信息 SharedPreferences
使用
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean(paintKey, paint).commit();
稍后阅读该数据
paint = settings.getBoolean(paintKey, false);
settings.getBoolean(paintKey,false);
此行从 SharedPreferences 获取一个值并立即忽略它。您必须将 return 值保存在变量中以便稍后使用它:
boolean paint = settings.getBoolean(paintKey,false);
这将创建一个只能在同一方法中使用的局部变量。如果您需要在其他方法中使用该值,请改为创建一个字段。