尝试使用 SharedPreferences 更改颜色,但方法不正确 [英] Trying to change colors with SharedPreferences but it is not getting in the right way

查看:65
本文介绍了尝试使用 SharedPreferences 更改颜色,但方法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从菜单中更改某些文本和 MainActivity 的颜色.我有两个菜单,白色和黑色,如果我点击白色将返回背景白色,按钮黑色,文本黑色.当我单击黑色时,将返回背景灰色、按钮白色、文本、白色的颜色.我正在尝试使用 SharedPreferences 保存它,但它并不总是工作,它总是采用灰色.如果我在没有 SharedPreferences 的情况下执行此操作并单击 PopUpMenu 它会更改颜色,但是使用 SharedPreferences 它不起作用我写的错误.

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.

这是我在 MainActivity.class 中的代码.

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);
    }

这是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;
        }
      });
    }
  });

推荐答案

你需要改变这个:

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
}

因为当 "getChangeColor()" 为 false 时,您将其设置为白色:

because when "getChangeColor()" is false you set it to white color:

switchOnOff = getChangeColor();
    if (switchOnOff) {
        setColorGreyImageButton();
    } else {
        setColorWhiteImageButton();

    }

这篇关于尝试使用 SharedPreferences 更改颜色,但方法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆