问题
I am trying to change colors of some text and MainActivity
from a menu.
I have two menus, white color and black color, if I click white will return the color of background white, buttons black, text black. When I click black will return the color of background grey, buttons white, text, white.
I am trying this to save with SharedPreferences
but it is not working always it takes the grey color.
If I do this without SharedPreferences
and click on the PopUpMenu
it changes the color, but with SharedPreferences
it is not working something I am writing false.
This is my code in MainActivity.class
.
private boolean switchOnOff;
public static final String Change_Color = "Change_Color";
switchOnOff = getChangeColor();
if (switchOnOff) {
setColorGreyImageButton();
} else {
setColorWhiteImageButton();
}
public void setColorGreyImageButton() {
settings.setColorFilter(Color.parseColor("#757575"));
voiceSearch.setColorFilter(Color.parseColor("#757575"));
share.setColorFilter(Color.parseColor("#757575"));
search.setColorFilter(Color.parseColor("#757575"));
mainView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
public void setColorWhiteImageButton() {
settings.setColorFilter(Color.parseColor("#FFFFFF"));
voiceSearch.setColorFilter(Color.parseColor("#FFFFFF"));
share.setColorFilter(Color.parseColor("#FFFFFF"));
search.setColorFilter(Color.parseColor("#FFFFFF"));
mainView.setBackgroundColor(Color.parseColor("#ff212121"));
}
public void saveColor(boolean changeColor) {
SharedPreferences sharedPreferences = getSharedPreferences("Color", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getPackageName() + ".change_color", changeColor);
editor.apply();
}
private boolean getChangeColor() {
SharedPreferences sharedPreferences = getSharedPreferences("Color", MODE_PRIVATE);
return sharedPreferences.getBoolean(getPackageName() + ".change_color", false);
}
This is the PopUpMenu
in MainActivity.class
mPopupMenu = new PopupMenu(this, settings);
MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.main_settings, mPopupMenu.getMenu());
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupMenu.show();
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if(id == R.id.menu_back_white) {
saveColor(true);
} else if (id == R.id.menu_back_black) {
saveColor(false);
}
return false;
}
});
}
});
回答1:
You need to change this:
if(id == R.id.menu_back_white) {
saveColor(false); //I changed true to false
} else if (id == R.id.menu_back_black) {
saveColor(true); //I changed false to true
}
because when "getChangeColor()" is false you set it to white color:
switchOnOff = getChangeColor();
if (switchOnOff) {
setColorGreyImageButton();
} else {
setColorWhiteImageButton();
}
来源:https://stackoverflow.com/questions/53943508/trying-to-change-colors-with-sharedpreferences-but-it-is-not-getting-in-the-righ