How catch click on ListPreference like in Android Browser Settings
The settings of the Android Browser allow to click on a ListPreference if one wants to clear the cache. If the users clicks on that ListPreference no list is shown - a dialog pops up instead.
I would like to use a similar feature to stop a running service.
I created an entry in the application settings without the android:entries and android:entryValues tags:
<ListPreference
android:key="list_stop"
android:title="@string/txt_stopservice" />
No I try to catch the click:
listPreferenceServiceStop = (ListPreference) prefe开发者_如何学PythonrenceScreen.findPreference("list_stop");
listPreferenceServiceStop.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Tools.showToast(context, "BlaBla");
return true;
}
});
If the user clicks on this entry the following error appears:
java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array.
The Android Browser settings don't show values neither.
How do they do that? And is this the correct way to catch the click?
Many thanks in advance. HJW
Why are you using a ListPreference? You should use a DialogPreference
instead.
ListPreference myVar = (ListPreference) findPreference("key you define at xml");
myVar.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Toast.makeText( getBaseContext(), "clicked", Toast.LENGTH_LONG ).show();
return true;
}
});
精彩评论