简体   繁体   中英

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) preferenceScreen.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;
        }

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM