如何在 Flex 3 中的 TextInput 上的 keyDownHandler 上设置文本属性

发布于 2024-12-09 09:29:27 字数 883 浏览 0 评论 0原文

简单直接。 我扩展了 mx.controls.TextInput 以创建具有不同行为的自定义组件。 我试图在 keyDownHandler() 上设置 text 属性,但由于某种原因它无法按我的预期工作。组件上的文本有点忽略了更改。 我正在使用 Flex 3.6。 下面是一个简单的示例代码,解释了发生了什么:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:customcomponent="com.test.customcomponent.*">
    <customcomponent:TextTest x="20" y="20"/>   
</mx:Application>

在 AS 类下面:

package com.test.customcomponent
{
    import flash.events.KeyboardEvent;

    import mx.controls.TextInput;

    public class TextTest extends TextInput
    {
        public function TextTest()
        {
            super();
        }

        override protected function keyDownHandler(event:KeyboardEvent):void{
            text = "lol. It doesn't work";
        }
    }
}

Simple and straight forward.
I extended a mx.controls.TextInput to create a custom component with a different behavior.
I'm trying to set the text property on the keyDownHandler(), and for some reason it doesn't work as I expected. The text on the component just kinda ignore the change.
I'm using Flex 3.6.
Down here is a simple example code that explains what's going on:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:customcomponent="com.test.customcomponent.*">
    <customcomponent:TextTest x="20" y="20"/>   
</mx:Application>

And below de AS class:

package com.test.customcomponent
{
    import flash.events.KeyboardEvent;

    import mx.controls.TextInput;

    public class TextTest extends TextInput
    {
        public function TextTest()
        {
            super();
        }

        override protected function keyDownHandler(event:KeyboardEvent):void{
            text = "lol. It doesn't work";
        }
    }
}

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

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

发布评论

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

评论(2

潜移默化 2024-12-16 09:29:27

您需要防止默认的按键事件。

    override protected function keyDownHandler(event:KeyboardEvent):void{
        text = "lol. It doesn't work";
        event.preventDefault();
        event.stopImmediatePropagation();
        event.stopPropagation()
    }

You need to prevent the default key down event.

    override protected function keyDownHandler(event:KeyboardEvent):void{
        text = "lol. It doesn't work";
        event.preventDefault();
        event.stopImmediatePropagation();
        event.stopPropagation()
    }
萌面超妹 2024-12-16 09:29:27

为了能够防止默认处理,您必须对处理程序具有足够高的优先级(而 keyDownHandler() 没有)。这意味着你需要注册自己的方法,优先级> 0.

你可以这样尝试:

public function MyTextInput() {
    addEventListener(KeyboardEvent.KEY_DOWN, yourHandler, 
                     false, EventPriority.BINDING, true);
    ...
}

private function yourHandler(event : KeyboardEvent) : void {
    // stop further handling
    event.preventDefault();
    event.stopImmediatePropagation();
    event.stopPropagation();  

    // do your work here
    text = ...;
}

In order to be able to prevent the default handling, you have to have a high enough priority on your handler (which keyDownHandler() does not have). This means you need to register your own method with priority > 0.

You can try like this:

public function MyTextInput() {
    addEventListener(KeyboardEvent.KEY_DOWN, yourHandler, 
                     false, EventPriority.BINDING, true);
    ...
}

private function yourHandler(event : KeyboardEvent) : void {
    // stop further handling
    event.preventDefault();
    event.stopImmediatePropagation();
    event.stopPropagation();  

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