flex4 中文做二个字节处理,超级简单实用

var control:TextInput= new TextInput;

      TextInput(control).maxChars= 10;
      TextInput(control).addEventListener(TextOperationEvent.CHANGING, function changingHandler(event:TextOperationEvent):void {
       if ((event.operation as InsertTextOperation) == null) {
        return;
       }
       var input:String = (event.operation as InsertTextOperation).text;
       if ((getStringBytesLength(control.text, "gb2312") + getStringBytesLength(input, 'gb2312')) > control.maxChars) {
        event.preventDefault();
       }
      });





private function getStringBytesLength(str:String, charSet:String):int {
   var bytes:ByteArray = new ByteArray();
   bytes.writeMultiByte(str, charSet);
   bytes.position = 0;
   return bytes.length;

  }

你可能感兴趣的:(flex4)