【问题标题】:android studio switch how to pro-grammatically set the state of the switchandroid studio switch 如何以编程方式设置开关的状态
【发布时间】:2020-05-18 13:51:17
【问题描述】:

我有一个开关,我检查它的状态,然后从中更新一个全局设置类。

但是,当我重新打开活动时,开关默认回到关闭状态。 如何在 oncreate 中设置它以从我的设置中提取。

为了拉动我的开关的布尔状态,我这样做:

Boolean setting_variable = global_settings.Get_Advanced_settings();

然后我想要类似的东西:

my_switch.state(setting_variable);//Set the switch state

【问题讨论】:

  • 使用首选项保存开关的状态。在 oncreate 中检查它,然后使用 my_switch.setChecked(boolean)
  • 正是我想要的。
  • 你需要它的代码吗?
  • 没关系,我只需要 API 调用。 switch_advanced_mode.setChecked(global_settings.Get_Advanced_settings());
  • 好的,将我的评论移至答案,请标记为正确

标签: java android-studio uiswitch


【解决方案1】:

使用首选项保存开关的状态。在 oncreate 中检查,然后使用 my_switch.setChecked(boolean)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-17
    • 2023-04-09
    • 1970-01-01
    • 2017-08-27
    • 2011-08-22
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多