从 Flex TextInput 控件捕获用户输入:使用哪个事件?

发布于 2024-07-08 02:41:06 字数 90 浏览 6 评论 0原文

我应该使用 changetextInput 事件来捕获 TextInput 控件上的用户输入吗? 为什么?

Should I use the change or textInput event to capture user input on a TextInput control? Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-07-15 02:41:07

CHANGE 不会告诉您发生了什么,但 TEXT_INPUTKEY_DOWN 会告诉您。

CHANGE doesnt tell you what has changed though - TEXT_INPUT and KEY_DOWN do.

零度° 2024-07-15 02:41:06

textInput 仅当用户在控件中输入文本时调度。 更改 在用户提交的每次更改时调度。 例如,如果用户删除了文本的一部分,则仅调度 change 事件。

通过代码修改文本时,不会调度这些:

flash.events.TextEvent.TEXT_INPUT

“当用户在文本中键入、删除或粘贴文本时调度
控制。”

- 我刚刚尝试过,当用户删除文本时不会调度此事件)

flash.events.Event.CHANGE

“当 TextInput 控件中的文本通过用户更改时调度
输入。 如果出现以下情况,则不会发生此事件
您使用数据绑定或 ActionScript
更改文本的代码
。”

您还可以使用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 the change event is dispatched.

Neither of these is dispatched when the text is modified via code:

flash.events.TextEvent.TEXT_INPUT:

"Dispatched when the user types, deletes, or pastes text into the
control."

(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:

"Dispatched when text in the TextInput control changes through user
input. This event does not occur if
you use data binding or ActionScript
code to change the text
."

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.

⊕婉儿 2024-07-15 02:41:06

这是一个很好的答案,哈塞格。 如果我有足够的代表,我会投票。

根据您捕获用户输入的目的,您可以对 TextInput 组件进行子类化并覆盖 Change 和 textInput 事件的内部侦听器。

我不知道您是否有很多原因想要这样做,但我最近这样做是为了处理 OS X 中的一个错误,该错误导致粘贴的换行符表示为“\r”,而不是“\” n'。

您需要做的就是在子类对象的构造函数中调用 super() 之后添加以下内容:

this.addEventListener(Event.CHANGE, textFieldChangeListener);   
this.addEventListener(TextEvent.TEXT_INPUT,textFieldInputListener);

然后添加侦听器方法和要执行的代码。

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:

this.addEventListener(Event.CHANGE, textFieldChangeListener);   
this.addEventListener(TextEvent.TEXT_INPUT,textFieldInputListener);

And then add the listener methods and the code you want to execute.

妖妓 2024-07-15 02:41:06

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文