当用户在框内放置一个值时,我正在使用此代码来应用editTextPreference标题。 问题是当我点击“确定”时它不会改变标题,但只有当我重新点击editTextPreference它才会应用之前写的值。
public void handleTEST(){
final EditTextPreference pref = (EditTextPreference)findPreference("test");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
pref.setTitle(pref.getText());
return true;
}
});
}
那么如何在“确定”点击上应用标题?
答案 0 :(得分:0)
符合问题的简短答案:
public class SomePrefenceScreen extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
private EditTextPreference edittext;
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.your_settings_xml);
edittext = (EditTextPreference) getPreferenceScreen().findPreference("test");
}
@Override
public void onStart() {
updateAll();
super.onStart();
}
@Override
public void onResume() {
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener(this);
super.onResume();
}
@Override
public void onPause() {
PreferenceManager.getDefaultSharedPreferences(context)
.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
updateAll();
}
private void updateAll() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
// test_key being the android:key defined in XML in your_settings_xml
String titleString = sharedPrefs.getString("test_key", "");
if (!titleString.isEmpty()) {
edittext.setTitle(titleString);
}
}
}
使用帮助程序类的长答案和更通用的方法:
public class GoogleMapsSettings extends DaggerPreferenceFragment implements
OnSharedPreferenceChangeListener {
private static final String TAG = "GoogleMapsSetings";
private StatusCheck statusCheck;
private ListPreference address_entry;
private ListPreference city_entry;
@Inject @ForActivity Context context;
@Inject Preferences preferences;
@Inject GoogleMapsModule googleMapsModule;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings_module_googlemaps);
address_entry = (ListPreference) getPreferenceScreen().findPreference(
getString(R.string.key_googlemaps_address_entry));
city_entry = (ListPreference) getPreferenceScreen().findPreference(
getString(R.string.key_googlemaps_city_entry));
}
@Override public void onStart() {
updateAllSummaries();
statusCheck = new StatusCheck(context, googleMapsModule, this);
statusCheck.runCheck();
super.onStart();
}
@Override public void onResume() {
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener(this);
super.onResume();
}
@Override public void onPause() {
PreferenceManager.getDefaultSharedPreferences(context)
.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override public void onSharedPreferenceChanged(SharedPreferences prefs,
String key) {
if (key.equals(getString(R.string.key_module_activated_googlemaps))) {
statusCheck.runCheck();
}
updateAllSummaries();
}
private void updateAllSummaries() {
SummaryUpdater.updateEntry(this, preferences,
R.string.key_googlemaps_address_entry,
R.string.prefs_setting_summary_address);
SummaryUpdater.updateEntry(this, preferences,
R.string.key_googlemaps_city_entry,
R.string.prefs_setting_summary_city);
}
SummaryUpdater只是一个更新相关文本的助手类,这有助于减少代码冗余:
public class SummaryUpdater {
public static void updateString(PreferenceFragment pref,
Preferences preferences, int resource) {
updateString(pref, preferences, resource, "");
}
public static void updateString(PreferenceFragment pref,
Preferences preferences, int resource, String postfix) {
Preference preference = (Preference) pref.findPreference(pref
.getString(resource));
if (preference != null) {
String summaryString = preferences.getString(resource);
summaryString = (postfix.isEmpty()) ? summaryString : summaryString
+ " " + postfix;
if (!summaryString.isEmpty()) {
preference.setSummary(summaryString);
}
}
}
public static void updateEntry(PreferenceFragment pref,
Preferences preferences, int resourcekey, int resourcedefault) {
Preference preference = pref
.findPreference(pref.getString(resourcekey));
if (preference != null) {
int currentEntry = Integer.parseInt(preferences
.getString(resourcekey));
if (currentEntry != 0) {
preference.setSummary(pref.getString(R.string.entry) + ": "
+ currentEntry);
} else {
preference.setSummary(pref.getString(resourcedefault));
}
}
}
public static void updateInt(PreferenceFragment pref,
Preferences preferences, int resource) {
updateInt(pref, preferences, resource, "");
}
public static void updateInt(PreferenceFragment pref,
Preferences preferences, int resource, String postfix) {
Preference preference = (Preference) pref.findPreference(pref
.getString(resource));
if (preference != null) {
String summaryString = "" + preferences.getStringInt(resource);
summaryString = (postfix.isEmpty()) ? summaryString : summaryString
+ " " + postfix;
preference.setSummary(summaryString);
}
}
}
Preferences对象只是访问sharedPreferences的读/写,这里是例如String值的get / set:
public void putString(int resource, String value) {
String key = context.getString(resource);
boolean success = sharedPrefs.edit().putString(key, value).commit();
}
public String getString(int resource) {
String key = context.getString(resource);
if (sharedPrefs != null && context.getString(resource) != null) {
return sharedPrefs.getString(key, "");
}
return "";
}
答案 1 :(得分:0)
据我所知,您只想在特定首选项的标题中显示优先存储的值。所以试试这个:
public void handleTEST(){
final EditTextPreference pref = (EditTextPreference) findPreference("test");
pref.setTitle(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("test", "Default Title"));
// Loads the title for the first time
// Listens for change in value, and then changes the title if required.
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
pref.setText(newValue.toString());
return false;
}
});
}
希望这会有所帮助:)