问题描述
我在 PreferenceActivity
中有一个 EditTextPreference
.当用户单击 EditTextPreference
时,将显示一个对话框.在对话框中,用户可以输入一个值,对话框有确定"和取消"按钮.我想调用ok按钮的点击事件来检查值,但我不知道如何调用点击事件.
I have an EditTextPreference
in the PreferenceActivity
. When user click the EditTextPreference
will show a dialog. In the dialog, user can input a value, and the dialog has "OK" and "Cancel" buttons. I want to call the click event of ok button to check the value, but I do not know how to call the click even.
我知道我可以使用 EditTextPreference.setOnPreferenceChangeListener()
,但我想知道我是否可以使用 OK 按钮点击事件.
I know I can use EditTextPreference.setOnPreferenceChangeListener()
, but I want to know if I can use OK button click event.
推荐答案
实际上你不能,因为首选项使用内部 AlertDialog.Builder
并且每次单击首选项时都会创建一个新对话框.下一个问题是对话框构建器为您设置了点击侦听器,如果您覆盖它们,您可能会破坏按钮点击的关闭行为.
Actually you can't since the preference is using an internal AlertDialog.Builder
and creates a new dialog every time you click the preference. The next problem is that the dialog builder sets the click listener for you and if you override them you might destroy the close behavior of the button click.
这让我很困扰,因为我想要一个仅在有效输入时关闭的首选项(否则会显示吐司,如果用户无法正确输入,则应按取消).
This bothered me since I wanted a preference which only closes on valid input (otherwise a toast is shown and user should press cancel if he can't get it right).
(如果您确实需要针对此问题的解决方案)您可以找到验证 DialogPreference
的通用解决方案 此处 和验证 EditTextPreference
这里 这是我自己写的.
(If you really need a solution for exactly this problem) You can find general solution of a validating DialogPreference
here and a validating EditTextPreference
here which I wrote myself.
这篇关于如何调用 EditTextPreference 中的确定按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!