我的活动中有Switch
,用户点击它即可启用或禁用设置。我将Switch当前条件存储在静态变量中。
现在,交换机始终处于ON状态,即使用户禁用它,它也会在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.
答案 0 :(得分:1)
您需要调用switchBtn.setChecked(false);
https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)