0

有人会帮助我吗?我有 8 个 CheckBoxPreference 和一个 ListPreference & 我似乎无法让它们设置默认值。附上我的preferences.xml、string.xml & Preference.java

首选项.xml

<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android">
<!-- Setting Preferences for FIRST Character on PASSWORD GENERATOR -->
<PreferenceCategory  android:title="@string/first_cat_pref_title" 
     android:key="@string/first_cat_pref_key" >
<CheckBoxPreference android:title="@string/pref_first_char_num_title"
android:defaultValue="@string/pref_first_char_num_default" 
android:summary="@string/pref_first_char_num_summary" 
android:key="@string/pref_first_char_num_key"  />
<CheckBoxPreference android:title="@string/pref_first_char_lc_title"
      android:defaultValue="@string/pref_first_char_lc_default"
android:summary="@string/pref_first_char_lc_summary" 
      android:key="@string/pref_first_char_lc_key" />
<CheckBoxPreference android:title="@string/pref_first_char_uc_title"
        android:defaultValue="@string/pref_first_char_uc_default"
android:summary="@string/pref_first_char_uc_summary" 
android:key="@string/pref_first_char_uc_key" />
<CheckBoxPreference android:title="@string/pref_first_char_other_title"
        android:defaultValue="@string/pref_first_char_other_default"
android:summary="@string/pref_first_char_other_summary" 
android:key="@string/pref_first_char_other_key" />
</PreferenceCategory>
<!-- Setting Preferences for Second Character on -->
<PreferenceCategory android:title="@string/sec_cat_pref_title" 
      android:key="@string/sec_cat_pref_key">
<CheckBoxPreference android:title="@string/pref_second_char_num_title"
android:defaultValue="@string/pref_second_char_num_default"
android:summary="@string/pref_second_char_num_summary" 
android:key="@string/pref_second_char_num_title" />
<CheckBoxPreference android:title="@string/pref_second_char_lc_title"
        android:defaultValue="@string/pref_second_char_lc_default"
android:summary="@string/pref_second_char_lc_summary" 
android:key="@string/pref_second_char_lc_key" />
<CheckBoxPreference android:title="@string/pref_second_char_uc_title"
        android:defaultValue="@string/pref_second_char_uc_default"
android:summary="@string/pref_second_char_uc_summary"   
android:key="@string/pref_second_char_uc_key" />
<CheckBoxPreference android:title="@string/pref_second_char_other_title"
        android:defaultValue="@string/pref_second_char_other_default"
android:summary="@string/pref_second_char_other_summary" 
android:key="@string/pref_second_char_other_key" />
</PreferenceCategory>
<!-- Third Preference Setting Length -->
<PreferenceCategory android:title="@string/third_cat_pref_title" 
      android:key="@string/third_cat_pref_key" >
  <ListPreference android:key="PGCharCountValue" android:title="@string/pg_char_count_title"
android:summary="@string/pg_char_count_summary" android:defaultValue="8"
android:entries="@array/PGCharCount" 
      android:entryValues="@array/PGCharCountValue"  />
</PreferenceCategory>
</PreferenceScreen>

字符串.xml

<!-- Preferences / Settings -->
<string name="menu_settings">Settings</string>
<!--  First category -->
<string name="first_cat_pref_title">First Character Preferences</string>
<string name="first_cat_pref_key">First_Char_Pref</string>
<!-- FIRST character FIRST check box  Preferences -->
<string name="pref_first_char_num_title">1ST Character Number</string>
<string name="pref_first_char_num_default">true</string>
<string name="pref_first_char_num_summary">Sets character possible for Number</string>
<string name="pref_first_char_num_key">First_Char_Num</string>
<!-- FIRST character SECOND check box  Preferences -->
<string name="pref_first_char_lc_title">1ST Character Lower</string>
<string name="pref_first_char_lc_default">true</string>
<string name="pref_first_char_lc_summary">Sets character possible for Lower</string>
<string name="pref_first_char_lc_key">First_Char_Lower</string>
<!-- FIRST character THIRD check box  Preferences -->
<string name="pref_first_char_uc_title">1ST Character Upper</string>
<string name="pref_first_char_uc_default">true</string>
<string name="pref_first_char_uc_summary">Sets character possible for Upper</string>
<string name="pref_first_char_uc_key">First_Char_Lower</string>
<!-- FIRST character FOURTH check box  Preferences -->
<string name="pref_first_char_other_title">1ST Character Other</string>
<string name="pref_first_char_other_default">false</string>
<string name="pref_first_char_other_summary">Sets character possible for Other</string>
<string name="pref_first_char_other_key">First_Char_Lower</string>
<!--  Second category -->
<string name="sec_cat_pref_title">2ND Character Preferences</string>
<string name="sec_cat_pref_key">Second_Char_Pref</string>
<!-- FIRST character FIRST check box  Preferences -->
<string name="pref_second_char_num_title">2ND Character Number</string>
<string name="pref_second_char_num_default">true</string>
<string name="pref_second_char_num_summary">Sets character possible for Number</string>
<string name="pref_second_char_num_key">Second_Char_Num</string>
<!-- FIRST character SECOND check box  Preferences -->
<string name="pref_second_char_lc_title">2ND Character Lower</string>
<string name="pref_second_char_lc_default">true</string>
<string name="pref_second_char_lc_summary">Sets character possible for Lower</string>
<string name="pref_second_char_lc_key">Second_Char_Lower</string>
<!-- FIRST character THIRD check box  Preferences -->
<string name="pref_second_char_uc_title">2ND Character Upper</string>
<string name="pref_second_char_uc_default">true</string>
<string name="pref_second_char_uc_summary">Sets character possible for Upper</string>
<string name="pref_second_char_uc_key">Second_Char_Lower</string>
<!-- FIRST character FOURTH check box  Preferences -->
<string name="pref_second_char_other_title">2ND Character Other</string>
<string name="pref_second_char_other_default">false</string>
<string name="pref_second_char_other_summary">Sets character possible for Other</string>
<string name="pref_second_char_other_key">Second_Char_Lower</string>
<!-- Set Default Password Count -->
<string name="third_cat_pref_title">Password length</string>
<string name="third_cat_pref_key">pswd_cnt_length</string>
<string name="pg_char_count_title">Default Password Length</string>
<string name="pg_char_count_summary">Default Characters count for Password</string>
<!-- Stop Default Char Count Password -->

首选项.java

package com.m4ltech.PasswordGenerator;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class PGPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pswd_preferences);
                PreferenceManager.setDefaultValues(this, R.xml.pswd_preferences, false);
 /**  I have tried*/
 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 boolean autoStart = sharedPreferences.getBoolean("pref_first_char_other_key", true);

 /** I have tried */
 boolean CheckboxPreference
 private void getPrefs() {
 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
 CheckboxPreference = prefs.getBoolean("pref_first_char_other_key", true); 
}
}

似乎没有任何工作?

4

1 回答 1

0

这似乎是Android CheckBoxPreference Default Value的副本,看起来这实际上是一个未解决的问题,请参见此处

解决方法在这里

于 2012-05-08T16:02:28.647 回答