Android的preferenceScreen

Android的preferenceScreen

本文介绍了Android的preferenceScreen onChangeListener未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的preferenceScreen,如下所示:

 公共类SettingsActivity扩展preferenceActivity实现在$ P $ {pferenceChangeListener@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    加preferencesFromResource(R.xml.settings);    preferenceScreen prefScreen = GET preferenceScreen();
    prefScreen.setOn preferenceChangeListener(本);}@覆盖
在preferenceTreeClick公共布尔(preferenceScreen preferenceScreen,preference preference){
    如果(preference.getTitle()。等于(的getString(R.string.settings_diverse_about))){
        // TODO关于对话框打开
    }    返回super.on preferenceTreeClick(preferenceScreen,preference);
}私人静态字符串PATTERN_EMAIL =[A-Z0-9 ._% - ] + @ [A-Z0-9 .-] + \\\\ [A-Z] {2,4};
私人静态字符串PATTERN_TWITTER =[A-ZA-Z0-9 _-] +;@覆盖
在preferenceChange公共布尔(preference preference,对象为newValue){
    字符串prefTitle = preference.getTitle()的toString()。    //手机将inputType下得到充分的处理    串中newstr = newValue.toString();    如果(prefTitle.equals(的getString(R.string.settings_contact_email))){
        返回newStr.trim()匹配(PATTERN_EMAIL)。
    }否则如果(prefTitle.equals(的getString(R.string.settings_contact_twitter))){
        返回newStr.trim()匹配(PATTERN_TWITTER)。
    }    返回true;
}

问题是,上preferenceChange方法被调用永远不会。
此外,你怎么看待有关验证属性的方式吗?
我觉得这不是真的最优的。

  preference.getTitle()。等于(的getString(R.string.settings_diverse_about))


解决方案

我有同样的问题。我试图建立一个在preferenceChangeListener但是这是错误的。我真正需要的是一个onShared preferenceListener。

  OnShared preferenceChangeListener侦听器;

您的preference活动在OnCreate

写这():

  =监听新的共享preferences.OnShared preferenceChangeListener(){
    公共无效onShared preferenceChanged(共享preferences preFS,串键){
        //这里您可以工作。
        //重要提示:当心,在这一点上,preference已经改变了!
    }
};共享preferences preFS = getShared preferences(getPackageName()+_ preferences,MODE_PRIVATE);
prefs.registerOnShared preferenceChangeListener(监听);

i wrote a simple PreferenceScreen, looks as follows:

public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    PreferenceScreen prefScreen = getPreferenceScreen();
    prefScreen.setOnPreferenceChangeListener(this);

}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference.getTitle().equals(getString(R.string.settings_diverse_about))) {
        // TODO open about dialog
    }

    return super.onPreferenceTreeClick(preferenceScreen, preference);
}

private static String PATTERN_EMAIL = "[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
private static String PATTERN_TWITTER = "[a-zA-Z0-9_-]+";

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    String prefTitle = preference.getTitle().toString();

    // phone will be fully handled by inputType

    String newStr = newValue.toString();

    if (prefTitle.equals(getString(R.string.settings_contact_email))) {
        return newStr.trim().matches(PATTERN_EMAIL);
    } else if (prefTitle.equals(getString(R.string.settings_contact_twitter))) {
        return newStr.trim().matches(PATTERN_TWITTER);
    }

    return true;
}

The problem is, the onPreferenceChange method gets never invoked.Furthermore, what do you think about the way of validating the attributes?I think it's not really optimal.

preference.getTitle().equals(getString(R.string.settings_diverse_about))
解决方案

I had the same problem. I tried to set up a onPreferenceChangeListener but that was wrong. What I really needed was an onSharedPreferenceListener.

OnSharedPreferenceChangeListener listener;

Write this in the onCreate() of your Preference activity:

listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // Here you can work.
        // IMPORTANT:  Beware that at this point the preference has already been changed!
    }
};

SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
prefs.registerOnSharedPreferenceChangeListener(listener);

这篇关于Android的preferenceScreen onChangeListener未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:54