如何在 EditTextPreference 更改时显示新值

发布于 2024-12-06 21:06:07 字数 2226 浏览 2 评论 0原文

我的 PreferenceActivity 中有两个 PreferenceActivity

我的问题是当我更新项目时,新值不会反映在屏幕上。

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");
        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;
    }

}

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");
        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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神也荒唐 2024-12-13 21:06:07

调用。

preference.notifyChanged();

当其数据发生变化且应重新绘制时

Call

preference.notifyChanged();

when its data changed and it should be redrawn.

抱着落日 2024-12-13 21:06:07

我对 setTextsetTitle< 之间感到困惑/code>

        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;
        }

已经完成了我想要的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文