【问题标题】:Change style using button and save this state使用按钮更改样式并保存此状态
【发布时间】:2019-01-02 16:51:45
【问题描述】:

我的应用有两种颜色样式。 我可以使用按钮更改它们,但我不知道如何将实际样式保存为国内样式。当我重新启动我的应用程序时,我选择了其他样式。 你知道我怎样才能保存这个状态吗?也许savedInstanceState? 感谢您的任何回复

【问题讨论】:

    标签: android layout styles savestate


    【解决方案1】:

    您是否考虑过保留更改?您可以像这样使用 SharedPreferences:

    // 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
    

    【讨论】:

    • 非常有趣。谢谢。我会尝试你的解决方案:)
    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多