从 Flex TextInput 控件捕获用户输入:使用哪个事件?
我应该使用 change
或 textInput
事件来捕获 TextInput 控件上的用户输入吗? 为什么?
Should I use the change
or textInput
event to capture user input on a TextInput control? Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CHANGE
不会告诉您发生了什么,但TEXT_INPUT
和KEY_DOWN
会告诉您。CHANGE
doesnt tell you what has changed though -TEXT_INPUT
andKEY_DOWN
do.textInput
仅当用户在控件中输入文本时调度。更改
在用户提交的每次更改时调度。 例如,如果用户删除了文本的一部分,则仅调度change
事件。通过代码修改文本时,不会调度这些:
flash.events.TextEvent.TEXT_INPUT
:- 我刚刚尝试过,当用户删除文本时不会调度此事件)
flash.events.Event.CHANGE
:您还可以使用
valueCommit
事件,当用户“提交”更改(通常通过将焦点移离文本字段)时调度,但请记住当以编程方式更改字段值时也会调度此事件。textInput
is dispatched only when the user has input text into the control.change
, on the other hand, is dispatched on every change committed by the user. So for example, if the user deletes a part of the text, only thechange
event is dispatched.Neither of these is dispatched when the text is modified via code:
flash.events.TextEvent.TEXT_INPUT
:(ignore the word "delete" there -- I just tried it and this event is not dispatched when text is deleted by the user)
flash.events.Event.CHANGE
:You can also use the
valueCommit
event, which is dispatched when the user "commits" the changes (usually by moving the focus away from the text field), but remember that this event is also dispatched when the field value is changed programmatically.这是一个很好的答案,哈塞格。 如果我有足够的代表,我会投票。
根据您捕获用户输入的目的,您可以对 TextInput 组件进行子类化并覆盖 Change 和 textInput 事件的内部侦听器。
我不知道您是否有很多原因想要这样做,但我最近这样做是为了处理 OS X 中的一个错误,该错误导致粘贴的换行符表示为“\r”,而不是“\” n'。
您需要做的就是在子类对象的构造函数中调用 super() 之后添加以下内容:
然后添加侦听器方法和要执行的代码。
That's a great answer, hasseg. If I had enough rep, I'd up-vote it.
Depending on what you're capturing the user input for, you could subclass the TextInput component and override the internal listeners for the change and textInput events.
I don't know if there are a lot of reasons you'd want to do this, but I did it recently to deal with a bug in OS X that causes pasted linebreaks to be represented as '\r', instead of '\n'.
All you need to do is add the following after your super() call in the constructor of your subclassed object:
And then add the listener methods and the code you want to execute.
Event.CHANGE 和 TextEvent.TEXT_INPUT 事件都会在每个键入的字符上触发。 如果您希望某个事件仅针对给定 TextInput 字段触发一次,请使用 FocusEvent.FOCUS_OUT。
与 Event.CHANGE 和 TextEvent.TEXT_INPUT 一样,此事件只会在用户输入时触发,不会在以编程方式更改值时触发。
Both Event.CHANGE and TextEvent.TEXT_INPUT events trigger on each character typed. If you want an event that will only trigger a single time for a given TextInput field, use FocusEvent.FOCUS_OUT.
Like Event.CHANGE and TextEvent.TEXT_INPUT, this event will only trigger for user input, not for programmatic changes to the value.