繁体   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