简体   繁体   中英

Android, show dialog when ListPreference item is clicked

Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.

It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.

However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.

You probably want to create a custom ListPreference . Basically you want to extend from ListPreference (see original here ), and provide a custom protected void onPrepareDialogBuilder(Builder builder) , in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.

Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.

Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.

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