Sto costruendo un'attività preferenziale in cui la maggior parte delle preferenze nell'elenco eseguirà codice e non modificherà direttamente SharedPreference. Il mio file preferences.xml ha questo aspetto.Attività preferenziale su Preferenza Clic listener
<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>
Il mio obiettivo è quello di avere un blocco di codice eseguito quando una di queste preferenze sono scattati. Simile alla "Cancella cronologia ricerche" nel menu delle preferenze di Google Play. (http://i.imgur.com/qnHbJX9.png)
Qualcuno sa come renderlo possibile?
Devo aggiungere che ho provato a utilizzare findPreference ("KeyNameHere") ma restituisce sempre null.
Grazie!
Edit:
ho aggiunto in questo codice e implementato OnPreferenceClickListener:
@Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
Ma questo metodo non viene mai chiamato. c'è un altro modo per fare ciò?
Edit 2:
ho scoperto che se prendo i tag PreferenceCategory così mi sono lasciato con questo:
<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" />
e chiamo questo:
getPreferenceScreen().setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
quindi ho effettivamente ricevuto una risposta dall'evento click. L'unico lato negativo è che devo rimuovere il raggruppamento delle preferenze. Qualcuno sa perché questo è e in qualsiasi modo per risolvere il problema?
+1 La tua soluzione mi ha suggerito ciò che ho scelto nella mia app. Faccio una matrice 'String' di chiavi e poi prendo' Preference' per ognuna e imposto 'onPreferenceClick'. È un codice di poche righe. Grazie. – Sufian
sebbene questo possa sembrare giusto, ma il metodo utilizzato era deprecato. Se hai una risposta aggiornata, modifica il tuo post. Nota getPreferenceScreen() è obsoleto. Grazie – ralphgabb