【问题标题】:How to show OFF state of switch from Code如何从代码中显示开关的关闭状态
【发布时间】:2017-02-01 01:29:03
【问题描述】:

我的活动中有一个Switch,用户单击它以启用或禁用设置。我将 Switch 当前状态存储在静态变量中。

现在,开关始终处于 ON 状态,即使用户禁用它,它在 Activity 上也已禁用,但当我重新启动 Activity 时,它处于打开状态。

我想知道如何从代码中设置OFF的开关状态。

<Switch
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/switchNotifications"
    android:layout_centerHorizontal="true"
    android:paddingTop="150dp"
    android:text="@string/notification_settings"
    android:textColor="@color/white"
    android:checked="true"
    android:textSize="18sp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:fontFamily="sans-serif-medium"
    android:theme="@style/SCBSwitch"
    />

JAVA

    switchBtn = (Switch) findViewById(R.id.switchNotifications);
    switchBtn.  //What to write here to OFF switch from CODE.

【问题讨论】:

  • switchBtn.setChecked(false)
  • stackoverflow.com/a/10577852/5871072此链接可能对您有所帮助
  • 默认情况下 android:checked="true" 设置为 false,但您在 xml 文件中将其设置为 true,您不应该这样做。

标签: android android-layout


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多