Preference Activity on Preference Click Listener

一个人想着一个人 提交于 2019-11-29 05:48:33

Implement OnPreferenceClickListener and in the onPreferenceClick

@Override
public boolean onPreferenceClick (Preference preference)
{
    String key = preference.getKey();
    // do what ever you want with this key
}

Maybe this could not be useful for OP, but could be useful for someone else. I'd like to write a sort of summary; in general, you can follow mainly three ways: 1) you can find your preference somewhere in your code with

Preference examplePreference = findPreference(KEY_EXAMPLE_PREFERENCE);

and then you can add a click listener and override its on click method with

examplePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        // handle click here
    }
});

This has to be done for every preference whose clicks you want to listen to 2) You can implement Preference.OnPreferenceClickListener interface in your settings fragment/activity and override onPreferenceClick just once, by using a switch construct or a if-else if-else if-... construct and merging all the single handlings; it should be something like:

@Override
public boolean onPreferenceClick(Preference preference) {
    switch (preference.getKey()) {
        case KEY_EXAMPLE_PREFERENCE: {
            // handle click here
        }
        break;
        case ...
    }
}

Then, you still have to find each preference but you can simply call on each of them

setOnPreferenceClickListener(this);

(I think the OP's implementation didn't work (his method wasn't called) because of this last part) we pass "this" as parameter because we implemented the click listener interface

3) (which I think is the easiest) you can override

onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)

in your preference fragment/activity without implementing any other interface and there you can copy the switch of the if-else if-... construct of option 2); the main advantage in that you shouldn't need to find each preference and to call on them setOnPreferenceClickListener.

Hope this will be useful for someone!

Just override:

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

    String key = preference.getKey();
    ...

    return super.onPreferenceTreeClick(preferenceScreen, preference);
}

I came up with my own (what I believe is really messed up) solution; but it works.

for(int x = 0; x < getPreferenceScreen().getPreferenceCount(); x++){
        PreferenceCategory lol = (PreferenceCategory) getPreferenceScreen().getPreference(x);
        for(int y = 0; y < lol.getPreferenceCount(); y++){
            Preference pref = lol.getPreference(y);
            pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }

            });
        }
    }

So what I have learned is there is a hierarchical system that works like: PreferenceScreen has children PreferenceCategory has children Preference, as you can see in the XML file. My problem was I could not set the preferences' onClickListeners directly from the PreferenceScreen. So I made two for loops that will get down to each Preference and set an OnPreferenceClickListener for each and every one of them. Messy, but works finally.

You could also find the preference and set the click listener.

Preference connectToNewComputer= findPreference("connectToNewComputer");
connectToNewComputer.setOnPreferenceClickListener(this);

Your Preference object wont get null if you will find followings (copypasting from the project):

public class ImePreferences extends PreferenceActivity {

.....

@Override
protected boolean isValidFragment(String fragmentName) {
    return Settings.class.getName().equals(fragmentName);
}

.....

public static class Settings extends InputMethodSettingsFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setInputMethodSettingsCategoryTitle(R.string.language_selection_title);
        setSubtypeEnablerTitle(R.string.select_language);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.ime_preferences);
        Preference pLcl = getPreferenceScreen().findPreference(getResources().getString(
                R.string.dictionary_button));
        pLcl.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                // handle click here
                l.a("this is the click");
                return true;
            }
        });
        if(pLcl != null)
            l.a(6576);
    }
}

.....

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!