2016-09-23 66 views
0

中顯示開關OFF狀態在我的活動中有一個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. 
+1

'switchBtn.setChecked(假)' –

+1

http://stackoverflow.com/a/10577852/5871072這個鏈接可以幫助可以幫助你 –

+1

默認情況下'機器人:檢查= 「真」 是'設置爲false,但是您在xml文件中將其設置爲true,您不應該這樣做。 –

回答