宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应

[AS3]as3侦听用户输入文本源代码示例

2016-10-13 00:16:48 点击量:14 标签: 收藏本文

通过TextEvent事件监听用户对文本框内容的修改,如删除,剪切,插入或者拷贝等操作,对文本框的每一次修改都会激活textInput事件,可以通过event.preventDefault()取消显示输入的文本

  1. package { 
  2.  import flash.display.Sprite; 
  3.  import flash.events.TextEvent; 
  4.  import flash.text.TextField; 
  5.  import flash.text.TextFieldType; 
  6.  public class Sample0414 extends Sprite 
  7.  { 
  8.   private var lblInfo:TextField; 
  9.    
  10.   public function Sample0414() 
  11.   { 
  12.    var textBox:TextField = new TextField(); 
  13.    textBox.type = TextFieldType.INPUT; 
  14.    textBox.background = true
  15.    textBox.width = 200
  16.    textBox.height = 20
  17.    textBox.addEventListener(TextEvent.TEXT_INPUT,onTextInput); 
  18.    this.addChild(textBox); 
  19.   } 
  20.    
  21.   private function onTextInput(event:TextEvent):void 
  22.   { 
  23.    if(event.text.toLowerCase().indexOf("<script>")>-1) 
  24.    { 
  25.     event.preventDefault(); 
  26.    } 
  27.   } 
  28.  }