开发者

How to show the new value when EditTextPreference is changed

开发者 https://www.devze.com 2023-04-08 05:31 出处:网络
I have two PreferenceActivity in my PreferenceActivity. My issue is when I update an item, new value is not reflected in the screen.

I have two PreferenceActivity in my PreferenceActivity.

My issue is when I update an item, new value is not reflected in the screen.

public class HostSettingActivity extends PreferenceActivity {

    private final String MY_DEBUG_TAG = "SettingActivity";
    SharedPreferences sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(MY_DEBUG_TAG, "HostSettingActivity Started");
        super.onCreate(savedInstanceState);
        sharedPrefs = getPreferenceManager().getSharedPreferences(); 
        setPreferenceScreen(createPreferenceHierarchy());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(MY_DEBUG_TAG, "On Destroy");
    }

    private PreferenceScreen createPreferenceHierarchy() {

        // Root
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);


        PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
        dialogBasedPrefCat.setTitle("Host Settings");
        root.addPreference(dialogBasedPrefCat);

        EditTextPreference hostPreference = new EditTextPreference(this);
        hostPreference.setKey("host");
        hostPreference.setDialogTitle("Host");
        hostPreference.setDefaultValue("http://example.com");
   开发者_如何学Go     hostPreference.setSummary("Set host");
        dialogBasedPrefCat.addPreference(hostPreference);

        EditTextPreference portPreference = new EditTextPreference(this);
        portPreference.setKey("port");
        portPreference.setDialogTitle("Port");
        portPreference.setDefaultValue("8080");
        portPreference.setSummary("Set port");
        dialogBasedPrefCat.addPreference(portPreference);

        hostPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                EditTextPreference etp = (EditTextPreference) preference;
                String newHostValue = newValue.toString();
                Log.i(MY_DEBUG_TAG, "New Host: "+newHostValue);
                etp.setText(newHostValue);
                return true;
            }
        });
        return root;
    }

}


Call

preference.notifyChanged();

when its data changed and it should be redrawn.


I was confused between setText and setTitle

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            EditTextPreference etp = (EditTextPreference) preference;
            String newHostValue = newValue.toString();
            Log.i(MY_DEBUG_TAG, "New Host: "+newHostValue);
            etp.setTitle(newHostValue);
            return true;
        }

has done what I want

0

精彩评论

暂无评论...
验证码 换一张
取 消