获取活动中CheckBoxPreference的值

时间:2010-09-06 21:29:32

标签: android checkbox android-activity preferences

我有一个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");
 }

}

希望任何人都可以提供帮助,谢谢:)

1 个答案:

答案 0 :(得分:4)

您需要使用在xml文件中设置的key属性。将android:key更改为“checkbox”而不是“inputPreferences”

相关问题