简体   繁体   English

Android,单击 ListPreference 项时显示对话框

[英]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.基本上我有一个 ListPreference 允许用户更改我的动态壁纸上某些文本的 X position。

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.它包含 4 个条目:顶部、中间、底部和手动输入 X。前 3 个选项没有问题,我只需在我的 WallpaperService class 中获取 SharedPreferences 并检查它们是顶部、中间还是底部并根据他们的选择更改 position .

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.但是,最后一个选项被证明更困难,我想要做的是当用户单击“手动输入 X”ListPreference 项时弹出一个 EditText 警报框,以便他们可以输入 X 的值。我只是不知道如何通过单击该特定列表元素弹出警报。

You probably want to create a custom ListPreference .您可能想要创建一个自定义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.基本上,您想从ListPreference扩展(参见原文here ),并提供自定义protected void onPrepareDialogBuilder(Builder builder) ,在其中您提供额外的“自定义”列表项和 onclick 来处理“自定义”条目的选择。

Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.请注意,我一直在说“自定义”,因为最好让这个 class 尽可能可重用。

Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.在您的PreferenceActivity中覆盖onPreferenceTreeClick()并将其提供的偏好与您想要为其做某事的偏好进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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