0
我正在尝试将EditTextPreference的值放入摘要中。有人可以告诉我如何做到这一点?它似乎不像我的OnSharedPreferenceChangeListener正在工作。PreferenceFragmentCompat无法识别首选项更改
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
public class OptionsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener{
private SharedPreferences sharedPreferences;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String s) {
// Adds preferences. See xml.preferences.
addPreferencesFromResource(R.xml.preferences);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("user_id")) {
Log.v("user_id", "is found");
Preference connectionPref = findPreference(key);
// Set summary to be the user-description for the selected value
connectionPref.setSummary(sharedPreferences.getString(key, ""));
}
}
}
我的preferences.xml:
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBoxPreference
android:key="gps_sync"
android:title="@string/gps_sync"
android:summary="@string/gps_sync_summ"
android:defaultValue="true"/>
<EditTextPreference
android:key="user_id"
android:title="@string/user_id"
android:summary=" "/>
请考虑接受答案作为正确的答案,如果它帮助你 – SoroushA