How to set edittext preference summary and have it stick

戏子无情 提交于 2021-01-27 03:37:40

问题


I have gone back and fourth on this and I just can not get it. I am setting up my settings using a preference fragment. I can get the settings to work and I can even get the "summary" to update when I make the change. But if I leave the settings screen and come back to it, the summary is back to the default text. So the question is, when using an edittext preference. How do you update the summary so it shows what the user changed the setting to and make it stick across closing the screen and app? In this case when my users change the mse_ip the summary changes to "MSE IP x.x.x.x" but as soon as I leave the settings screen and come back it is back to "0.0.0.0" which is what @string/mseip is set to.

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">     
    <EditTextPreference
            android:key="mse_ip"
            android:title="MSE IP"
            android:summary="@string/mseip"
            android:defaultValue="0.0.0.0"
            android:dialogTitle="IP Address for mse" />
    <EditTextPreference
            android:key="mse_username"
            android:title="Username"
            android:summary="MSE Username %s"
            android:defaultValue="Admin"
            android:dialogTitle="Username for mse" />
    <EditTextPreference
            android:key="mse_password"
            android:title="MSE Password"
            android:password="true"
            android:summary="******"
            android:defaultValue="Admin"
            android:dialogTitle="Password for mse" />
</PreferenceScreen>

preferencesfragment

package com.hmkcode.android;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

/*public class PrefsFragment extends PreferenceFragment {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
 } 
} */
public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // set texts correctly
    onSharedPreferenceChanged(null, "");

}

@Override
public void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    updatePreference(key); }
     private void updatePreference(String key){
            if (key.equals("mse_ip")){
                Preference preference = findPreference(key);
                if (preference instanceof EditTextPreference){
                    EditTextPreference editTextPreference =  (EditTextPreference)preference;
                    if (editTextPreference.getText().trim().length() > 0){
                        editTextPreference.setSummary("MSE IP  " + editTextPreference.getText());
                    }else{
                        editTextPreference.setSummary("MSE IP Not");
                    }
                }
            }
        }

}

setpreferenceactivity

import android.os.Bundle;
import android.app.Activity;

public class SetPreferenceActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content,
            new PrefsFragment()).commit();

    //setContentView(R.layout.activity_set_preference);
}
 }

回答1:


You have to set an OnPreferenceChangeListener to your Preference. So on every preference change, calling setSummary, changes the summary display. This might be an example code:

final Preference pref = getPreferenceManager().findPreference(
                PREF_KEY);
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                pref.setSummary(newValue.toString());
                return true;
            }
        });

You should also call your preference's setSummary method in onCreate(), so that Summary displays your sharedPreference value.




回答2:


By using androidx preference library (see the official guides), adding the following attribute is enough:

<EditTextPreference
    ...
    app:useSimpleSummaryProvider="true" />



回答3:


It seems that you just update the value of that EditText. Try to store the new value in the shared preferences by calling the

sharedPreferences.edit().putString(key, editTextPreference.getText()).apply();

This can be called in the updatePreference(String key) function.



来源:https://stackoverflow.com/questions/22651653/how-to-set-edittext-preference-summary-and-have-it-stick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!