0

我在活动中使用此代码

 protected void onResume() {
    super.onResume();
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    System.out.println(connections);
    String dir = prefs.getString("download_directory", "Download");
    System.out.println(dir);        
 }

我的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="Downloads">
        <EditTextPreference
            android:key="download_directory"
            android:title="@string/download_destination"
            android:summary="@string/download_description"
            android:defaultValue="FacebookPhotos"
            android:dialogTitle="@string/download_destination" />
        <CheckBoxPreference android:key="subdir_album"
            android:title="@string/download_album"
            android:summary="@string/download_album_description" />
     </PreferenceCategory>
  <PreferenceCategory android:title="Gallery">
     <EditTextPreference 
      android:key="connections"
      android:title="Concurrent connections"
      android:numeric="integer"
      android:maxLength="2" 
      android:defaultValue="7"
      android:dialogTitle="Concurrent connections" />
  </PreferenceCategory>
</PreferenceScreen>

编辑者

public class Preferences extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}
}

问题是,当我得到“连接”值时,它给了我正确的 int,而不是键“download_directory”的 getString 总是给我默认值“下载”

4

1 回答 1

0

键“download_directory”的getString总是给我默认值“下载”

仅当根本没有设置“download_directory”时才会发生这种情况!

于 2012-09-28T13:29:17.010 回答