EditTextPreference.setText(value) 未按预期更新 [英] EditTextPreference.setText(value) not updating as expected
问题描述
我试图阻止用户将空字符串输入到 EditTextPreference
(在示例中为 catName
).我使用 OnPreferenceChangeListener
来检测何时对 EditTextPreference
进行更改,如果有更改且字符串为空,我使用 EditTextPreference.setText()
命令重置为旧值.但是,如果我在 GUI 中重新打开 EditTextPreference
(字符串为空),新值不会正确显示,如果我回到主应用程序,我可以验证一个空值正在保存到首选项.
I'm trying to prevent the user from entering an empty string into an EditTextPreference
(in the example, catName
). I use a OnPreferenceChangeListener
to detect when a change is made to the EditTextPreference
, and if there is a change and the string is blank, I use the EditTextPreference.setText()
command to reset to the old value. However, the new value doesn't show up properly if I reopen the EditTextPreference
in the GUI (the string is blank), and if I go back into the main app, I can verify that a blank value is being saved to the preferences.
我已经验证 if 语句按预期执行,并且我的参数跟踪旧名称 (oldCatName
) 正在按预期更新.我什至可以在 setOnPreferenceChangeListener
完成执行之前记录 catName.getText()
值,我总是看到我期望的值(用户设置的新值,以及何时他们输入一个空白值,它会正确地重置为旧值).我不确定为什么将值设置为 EditTextPreference
不会将该值保存到首选项文件或更新 GUI.
I've verified that the if statement executes as expected, and that my parameter keeping track of the old name (oldCatName
) is updating as expected. I can even log the catName.getText()
value right before the setOnPreferenceChangeListener
finishes execution and I always see the value I expect (the new value set by the user, and when they enter a blank value, it properly resets to the old value). I'm not sure why setting the value to the EditTextPreference
isn't saving the value to the preferences file or updating the GUI.
public class SettingsActivity extends PreferenceActivity {
private String oldCatName;
private EditTextPreference catName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
catName = (EditTextPreference) findPreference("cat_name");
oldCatName = catName.getText();
catName.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
final String value = (String) newVal;
if (value.equals("")) {
catName.setText(oldCatName);
Log.e("new value", catName.getText());
}
else
oldCatName = value;
return true;
}
});
}
}
感谢您的帮助!-迈克尔
Thanks for the help! -Michael
澄清.if 语句中的逻辑正在正确执行.EditTextPreference
的字符串值甚至正在正确更新.但是,如果用户再次点击 EditTextPreference
,GUI 中的值不会正确更新,并且应用程序共享首选项中的值不会正确更新.它保持空白.
A clarification. The logic in the if statement is executing correctly. The string value of the EditTextPreference
is even updating correctly. However, the value in the GUI if the user taps on the EditTextPreference
again does not correctly update, and the value in the app's shared preferences does not update correctly. It stays blank.
推荐答案
最终通过以下操作找到了解决方案:
Finally found a solution by doing the following:
我使用了
SharedPreferences.OnSharedPreferenceChangeListener
而不是Preference.OnPreferenceChangeListener
.Preference.OnPreferenceChangeListener
查找用户何时通过设置菜单更改首选项,并在更改提交到首选项数据之前执行.SharedPreferences.OnSharedPreferenceChangeListener
侦听实际首选项数据的更改,而不是 GUI 中的更改,因此它会在稍后发生.我注意到在我的第一次尝试中,我可以在我的EditTextPreference
对象上运行setText()
紧跟其后的getText()
和>getText()
值与我刚刚设置的setText()
值不匹配.我不确定为什么会发生这种情况,但是在尝试运行setText()
之前等待更改实际提交会导致正确的响应.也许是时间问题?
I used a
SharedPreferences.OnSharedPreferenceChangeListener
instead of aPreference.OnPreferenceChangeListener
. ThePreference.OnPreferenceChangeListener
looks for when the user changes a preference through the settings menu, and behaves before the change is committed to the preference data. TheSharedPreferences.OnSharedPreferenceChangeListener
listens for changes to the actual preference data, not changes in the GUI, so it happens a little later. I noticed that in my first attempt, I could runsetText()
immediately followed bygetText()
on myEditTextPreference
object, and thegetText()
value wouldn't match what I had just set thesetText()
value to. I'm not sure why this happens, but waiting for the changes to actually commit before trying to runsetText()
led to the correct response. Maybe it was a timing issue?
我在更新 EditTextPreference
中的值后运行 setPreferenceScreen(null)
和 addPreferencesFromResource(R.xml.settings)
.如果我不这样做,有时当用户再次单击 EditTextPreference
时,即使设置文件中的值不是,该字段中的值也会显示为空白.这会强制设置页面或多或少地自行刷新.
I run setPreferenceScreen(null)
and addPreferencesFromResource(R.xml.settings)
after updating the value in the EditTextPreference
. If I didn't do this, sometimes when the user would click on the EditTextPreference
again, the value in the field would appear blank even though the value in the settings file wasn't. This forces the settings page to, more or less, refresh itself.
工作代码如下:
public class SettingsActivity extends PreferenceActivity {
private String oldCatName;
private EditTextPreference catName;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
createListener();
catName = (EditTextPreference) findPreference("cat_name");
oldCatName = catName.getText();
}
private void createListener() {
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
String value = sharedPreferences.getString("cat_name", "NULL");
if (value.equals("")) {
catName.setText(oldCatName);
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.settings);
} else {
oldCatName = value;
}
}
};
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.registerOnSharedPreferenceChangeListener(listener);
}
}
这篇关于EditTextPreference.setText(value) 未按预期更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!