本文介绍了在android系统preference活动监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code为prefernceactivity添加监听。但它不工作。
 请给出这方面的想法。

 公共类preference扩展preferenceActivity实现OnShared $ P $ {pferenceChangeListener
OnShared preferenceChangeListener侦听器;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    加preferencesFromResource(R.xml.settings);
    }
@覆盖
公共无效onShared preferenceChanged(共享preferences共享preferences,
        字符串键){
    // TODO自动生成方法存根}
}


解决方案

您必须先注册你的听众。

最好的办法是在 onResume 进行注册和注销的onPause

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // ...
    preFS = preferenceManager.getDefaultShared preferences(本);
}@覆盖
保护无效onResume(){
    super.onResume();
    prefs.registerOnShared preferenceChangeListener(本);
}@覆盖
保护无效的onPause(){
    super.onPause();
    prefs.unregisterOnShared preferenceChangeListener(本);
}

I am using the following code for adding a listener in prefernceactivity. But its not working. Please give a idea for this.

public class Preference extends PreferenceActivity implements OnSharedPreferenceChangeListener {
OnSharedPreferenceChangeListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    }
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    // TODO Auto-generated method stub

}


}
解决方案

You have to register your listener first.

The best way is to register it in onResume and unregister in onPause :

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
}

@Override
protected void onResume() {
    super.onResume();
    prefs.registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {      
    super.onPause();
    prefs.unregisterOnSharedPreferenceChangeListener(this);
}

这篇关于在android系统preference活动监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:38