我有问题,这是我第一次使用PreferenceFragment,当我得到数据时 它是空的我不知道问题出在哪里 这是我的PreferenceFragment和strings.xml和settings.xml文件 请帮忙!
PreferenceFragment
public class SettingsFragment extends PreferenceFragment {
private EditTextPreference mEditDomainPrefernce;
private EditTextPreference mEitDomainPrefernce2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
mEditDomainPrefernce = (EditTextPreference) getPreferenceManager().findPreference("ArabicDomain");
mEitDomainPrefernce2 = (EditTextPreference) getPreferenceManager().findPreference("KoreanDomain");
mEitDomainPrefernce2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mEitDomainPrefernce2.setSummary(newValue.toString());
return true;
}
});
mEditDomainPrefernce.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mEditDomainPrefernce.setSummary(newValue.toString());
return true;
}
});
}
}
的strings.xml
<resources>
<string name="app_name">My Application</string>
<string-array name="input">
<item>English</item>
</string-array>
<string-array name="out">
<item>Arabic</item>
<item>Korean</item>
</string-array>
<string name="default_arabic">http://somelink</string>
<string name="default_Korean">http://somelink</string>
</resources>
的settings.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="domain setting">
<EditTextPreference
android:key="ArabicDomain"
android:title="Arabic Domain"
android:summary="@string/default_arabic"
android:dialogTitle="change the domain"
android:defaultValue="@string/default_arabic"
/>
<EditTextPreference
android:key="KoreanDomain"
android:title="Korean Domain"
android:summary="@string/default_Korean"
android:dialogTitle="change the domain"
android:defaultValue="@string/default_Korean"
/>
</PreferenceCategory>
</PreferenceScreen>
最后我调用了Activity中的getDefaultSharedPreferences
mArabicDomain = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("ArabicDomain","");
mKoreanDomain = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("KoreanDomain","");
结果两个字符串都是空的,请告诉我有什么问题
答案 0 :(得分:1)
传递给getDefaultSharedPreferences
的第二个参数对应于defalut值。
而不是&#34; &#34;尝试&#34;一些字符串&#34;。
编辑:
试试这个:
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
EditTextPreference edp = (EditTextPreference)findPreference("KoreanDomain");
sharedPref.getString("KoreanDomain","defValue");