Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

android - Preference Activity on Preference Click Listener

I am building a Preference Activity where most of the preferences in the list will be executing code and not modifying a SharedPreference directly. My preferences.xml file looks like this.

<PreferenceCategory
    android:title="Connection" >

    <Preference
        android:id="@+id/settings_connectToNewComputer"
        android:key="connectToNewComputer"
        android:summary="Currently connected to:"
        android:title="Connect to new computer" />

    <Preference
        android:id="@+id/removeDevice"
        android:key="removeDevice"
        android:summary="Remove this device from the computer's whitelist"
        android:title="Remove this device from computer" />

</PreferenceCategory>

<PreferenceCategory
    android:title="About" >

    <Preference
        android:id="@+id/settings_About"
        android:key="about"
        android:summary="About me and my thanks to those who made this app great"
        android:title="About Hue Pro" />

    <Preference
        android:id="@+id/contact"
        android:key="contact"
        android:summary="Contact me with comments, bugs, and suggestions for updates"
        android:title="Contact me" />

</PreferenceCategory>

My goal is to have a block of code executed when a one of these preferences are clicked. Similar to the "Clear search history" in the Google Play settings preference menu. (http://i.imgur.com/qnHbJX9.png)

Does anyone know how to make this possible?

I have to add that I have tried using findPreference("KeyNameHere") but it always returns null.

Thank you!


Edit:

I added in this code and implemented OnPreferenceClickListener:

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

But this method never gets called. Is there another way to do this?


Edit 2:

I have found that if I take out the PreferenceCategory tags so I am left with this:

<Preference
    android:id="@+id/settings_connectToNewComputer"
    android:key="connectToNewComputer"
    android:summary="Currently connected to:"
    android:title="Connect to new computer" />

<Preference
    android:id="@+id/removeDevice"
    android:key="removeDevice"
    android:summary="Remove this device from the computer's whitelist"
    android:title="Remove this device from computer" />

<Preference
    android:id="@+id/settings_About"
    android:key="about"
    android:summary="About me and my thanks to those who made this app great"
    android:title="About Hue Pro" />

<Preference
    android:id="@+id/contact"
    android:key="contact"
    android:summary="Contact me with comments, bugs, and suggestions for updates"
    android:title="Contact me" />

and call this:

getPreferenceScreen().setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

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

then I actually get a response from the click event. The only down side is I have to remove the preference grouping. Anyone know why this is and any way to fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Implement OnPreferenceClickListener and in the onPreferenceClick

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...