Mar 09

下面的例子展示了如何通过监听TextArea的textInput事件和检查TextEvent对象的text属性,来截获该控件中按下的回车换行。

下面是具体的源代码:


Download: main.mxml
  1. <!-- http://blog.flexexamples.com/2008/03/07/preventing-line-feeds-in-a-textarea-control-in-flex/ -->
  2. <mx:application xmlns:mx="http://www.adobe.com/2006/mxml">
  3. layout="vertical"
  4. verticalAlign="middle"
  5. backgroundColor="white"&gt;</mx:application>
  6.  
  7. <mx:script>
  8. <!--[CDATA[
  9. private function textArea_textInput(evt:TextEvent):void {
  10. if (evt.text == "\n") {
  11. evt.preventDefault();
  12. }
  13. }
  14. ]]-->
  15. </mx:script>
  16.  
  17. <mx:textarea id="textArea">
  18. verticalScrollPolicy="on"
  19. width="160"
  20. height="120"
  21. textInput="textArea_textInput(event);"&gt;
  22. <mx:text>The quick brown fox jumped over the lazy dog.</mx:text>
  23. </mx:textarea>

下面是执行实例:

原文作者:Peter deHaan 翻译:minidxer

written by Minidxer  |  tags: , , , ,

Related Post

One Response to “如何通过监听textInput事件截获Flex的TextArea控件的回车”

  1. paravoice Says:

    为什么呢,直接监听不行么?
    TextInputのEventsのリスト
    Event Summary Defined By
    change  Dispatched when text in the TextInput control changes through user input. TextInput
    dataChange Dispatched when the data property changes. TextInput
    enter  Dispatched when the user presses the Enter key. TextInput
    textInput  Dispatched when the user types, deletes, or pastes text into the control. TextInput

Leave a Reply