Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 Flex:preventDefault不阻止添加字符_Actionscript 3_Apache Flex_Actionscript - Fatal编程技术网

Actionscript 3 Flex:preventDefault不阻止添加字符

Actionscript 3 Flex:preventDefault不阻止添加字符,actionscript-3,apache-flex,actionscript,Actionscript 3,Apache Flex,Actionscript,如果文本的宽度大于指定的数量,我希望防止将字符添加到TextInput。我遇到的第一个问题是,preventDefault没有阻止像我预期的那样添加任何字符。对此有何解释?我怎样才能实现我想要的 <s:TextInput id="textInput0"/> protected function creationCompleteHandler(event:FlexEvent):void { textInput0.addEventListener(KeyboardEvent.K

如果文本的宽度大于指定的数量,我希望防止将字符添加到
TextInput
。我遇到的第一个问题是,
preventDefault
没有阻止像我预期的那样添加任何字符。对此有何解释?我怎样才能实现我想要的

<s:TextInput id="textInput0"/>

protected function creationCompleteHandler(event:FlexEvent):void
{
    textInput0.addEventListener(KeyboardEvent.KEY_DOWN, checkWidthOfText, false, 0, true);
}
protected function checkWidthOfText(event:KeyboardEvent):void{

    event.preventDefault();     
} 

受保护的函数creationCompleteHandler(事件:FlexEvent):无效
{
textInput0.addEventListener(KeyboardEvent.KEY_DOWN,checkWidthOfText,false,0,true);
}
受保护函数checkWidthOfText(事件:KeyboardEvent):无效{
event.preventDefault();
} 
preventDefault()
只有当您试图更改其行为的组件实际实现了某种预防机制时,才会起作用。更具体地说,在这种情况下,文本字段(我说文本字段是因为Flex TextInput使用核心闪存)必须执行以下操作:

if (!keyDownEvt.isDefaultPrevented()) insertKey(keyDownEvt.charCode);
但它可能没有

至于问题的解决方案:只需使用TextInput的
maxChars
属性即可:

<s:TextInput maxChars="20"/>


或者你真的是指物理宽度而不是字符数吗?

只需使用
capture
event阶段和
TextEvent。TEXT\u输入
event:

        protected function creationCompleteHandler(event:FlexEvent):void
        {
            textInput0.addEventListener(TextEvent.TEXT_INPUT, checkWidthOfText, true, 0, true);
        }
        protected function checkWidthOfText(event:TextEvent):void{

            event.preventDefault();     
        } 

是的,这是正确的,但对于flex TextInput,它不使用自己的输入处理逻辑,它只使用type=input包装本机TextField,因此在这种情况下,您可以将其作为TextField进行操作。