gpt4 book ai didi

android - 更改 EditTextPreference 时如何显示新值

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:53 25 4
gpt4 key购买 nike

我的 PreferenceActivity 中有两个 PreferenceActivity

我的问题是当我更新一个项目时,新值没有反射(reflect)在屏幕上。

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

}

最佳答案

打电话

preference.notifyChanged();

当它的数据改变并且应该重绘时。

关于android - 更改 EditTextPreference 时如何显示新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7568848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com