簡體   English   中英

如何使用具有boolean,true和false的共享首選項

[英]How to use shared preferences with boolean , true and false

我使用以下代碼隱藏具有SharedPreferences的視圖,並帶有getVisibilitysetVisibility ,並添加了VISIBLE和GONE。 但是我現在想將SharedPreferences與CheckBox一起使用true / false布爾值,並使用setChecked 誰能幫我 ?

if (btnsearch.getVisibility() == View.VISIBLE) {
    btnsearch.setVisibility(View.GONE);
    SharedPreferences.Editor editor = sharedPreferences.edit();  
    editor.putBoolean("visibilitySearch", true).commit();
}

使用下面的代碼通過共享首選項獲取布爾值,

Boolean visibilitySearch= sharedPreferences.getBoolean("visibilitySearch", false);

用這種方式。

boolean shouldItBeVissible = sharedPreferences.getBoolean("visibilitySearch",default_value in case of key not found(true or false));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM