开发者

Get Value of a CheckBoxPreference in an Activity

开发者 https://www.devze.com 2023-01-15 14:53 出处:网络
I\'ve got a preferences.xml: <?xml version=\"1.0\" encoding=\"utf-8\"?> <PreferenceScreen xmlns:android=\"http://schemas.android.com/apk/res/android\">

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() {
 开发者_JAVA百科// 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 :)


you need to be using the key attribute that you set in your xml file. Change the android:key to "checkbox" rather than "inputPreferences"

0

精彩评论

暂无评论...
验证码 换一张
取 消