获取活动中 CheckBoxPreference 的值
我有一个preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Category">
<CheckBoxPreference
android:key = "inputPreferences"
android:title = "Title"
android:summary = "Subtitle"/>
</PreferenceCategory>
</PreferenceScreen>
我想读出CheckBoxPreference的值,并且根据它,应该显示(例如)一个TextView。我尝试了以下代码,但它不起作用:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
if(myPreference.getBoolean("checkbox", false)) {
VarText.setVisibility(View.VISIBLE);
VarText.setText("foo");
}
}
希望任何人都可以提供帮助,谢谢:)
I've got a preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Category">
<CheckBoxPreference
android:key = "inputPreferences"
android:title = "Title"
android:summary = "Subtitle"/>
</PreferenceCategory>
</PreferenceScreen>
I want to read out the value of the CheckBoxPreference, and depending on it, there sould be shown (for example) a TextView. I tried following code, but it doesn't work:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
if(myPreference.getBoolean("checkbox", false)) {
VarText.setVisibility(View.VISIBLE);
VarText.setText("foo");
}
}
Hope anyone can help, thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用在 xml 文件中设置的 key 属性。将 android:key 更改为“checkbox”而不是“inputPreferences”
you need to be using the key attribute that you set in your xml file. Change the android:key to "checkbox" rather than "inputPreferences"