代码之家  ›  专栏  ›  技术社区  ›  Jacek

使用按钮更改样式并保存此状态

  •  0
  • Jacek  · 技术社区  · 6 年前

    我的应用程序有两种颜色样式。 我可以用按钮来改变它们,但我不知道如何将真正的风格保存为国内风格。当我重新启动我的应用程序时,我选择了其他风格。 你知道我怎样才能挽救这个州吗?也许吧 savedInstanceState ? 谢谢你的回复

    1 回复  |  直到 6 年前
        1
  •  1
  •   user1581432    6 年前

    你有没有想过要坚持改变?您可以像这样使用共享的引用:

    // While choosing new style
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putInt("style", styleId);
    editor.commit(); // commit changes
    
    // While retriving choosen style
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    int styleId = pref.getInt("style", null); // null is default value
    setStyle(styleId); // custom method