设置默认CheckBoxPreference和ListPreference

时间:2012-02-28 01:28:02

标签: android default listpreference setting checkboxpreference

有人请帮帮我吗?我有8个CheckBoxPreference和一个ListPreference&我似乎无法让他们设置默认值。闭包是我的偏好.xml,string.xml& Preference.java

Preference.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>

string.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 -->

Preference.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); 
}
}

似乎没什么工作?

1 个答案:

答案 0 :(得分:0)

这似乎与Android CheckBoxPreference Default Value重复,看起来这实际上是一个未解决的问题,请参阅here

解决方法是here