OnSharedPreferenceChangeListener未注册首选项更改 [英] OnSharedPreferenceChangeListener not registering change in preference
本文介绍了OnSharedPreferenceChangeListener未注册首选项更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理列表首选项,并且已经实现了OnSharedPreferenceChangeListener,以基于在列表首选项上选择了哪个项目来更改文本视图上的文本.但是,它没有进行任何更改,并且我的想法已用尽,因此,我们将不胜感激.谢谢
I'm working on listpreferences and I have implemented the OnSharedPreferenceChangeListener to change the text on a textview based on which item is selected on the listpreference. However it doesn't register any changes and I have run out of ideas so any help would be appreciated. Thanks
public class pref extends PreferenceActivity implements OnSharedPreferenceChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.textView1);
String keys = null;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
keys = pref.getString("listPref1", "");
if (keys == "ON")
{
text.setText("ON");
}
else if (keys == "OFF")
{
text.setText("OFF");
}
else{
text.setText("Choose One");
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
//What code to I put in here to have the listener register the change
}
}
推荐答案
您的代码看起来应该可以工作:您可以尝试注销以确保其工作正常.将所有条件代码移到侦听器中.
Your code looks like it should work: you can try logging out to ensure its doing what it should. Move all the conditional code into the listener.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
android.util.Log.v("PREFERENCE CHANGE", "[" + key + "]");
if ("listPref1".equals(key)) {
String keys = pref.getString("listPref1", "");
if (keys == "ON") {
text.setText("ON");
} else if (keys == "OFF") {
text.setText("OFF");
} else {
text.setText("Choose One");
}
}
}
这篇关于OnSharedPreferenceChangeListener未注册首选项更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文