强制组件重绘它的焦点

时间:2011-03-23 13:23:48

标签: flex focus redraw

我还有一个“我如何”的问题:) 假设我有一个组件,并希望在运行时更改它的焦点颜色。这是你的一个例子(我已经排除了任何按钮,以防止组件失去它的焦点,因为在这种情况下它会完美地改变它的颜色):

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                creationComplete="init()">
    <mx:Script><![CDATA[    
        private function init():void {
            //putting focus inside needed component
            focusTest.setFocus();
            focusTest.setSelection(0, 0);

            // creates a new Timer
            var minuteTimer:Timer = new Timer(1000, 30);

            // designates listeners for the interval and completion events
            minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
            minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

            // starts the timer ticking
            minuteTimer.start();
        }    
        public function onTick(event:TimerEvent):void {
            // displays the tick count so far
            trace("tick " + event.target.currentCount);
            if (focusTest.getStyle('focusColor') != 0x008CEA) {
                focusTest.setStyle('focusColor', 0x008CEA);
            } else {
                focusTest.setStyle('focusColor', 0xFF0000);
            }
            //Update everything somehow :)            
        }    
        public function onTimerComplete(event:TimerEvent):void {
            trace("Time's Up!");
        }            
        ]]></mx:Script>
    <mx:TextInput id="focusTest"/>
</mx:Application>

我有什么:计时器正在滴答作响。该属性正在发生变化(您可以看到,例如,当您在浏览器中切换标签时......只要在颜色发生变化时捕捉到正确的状态)。

我想要什么:如何在没有魔法的情况下重新绘制焦点(我已经尝试了所有方法,从“validate”开始,我试过调用{ {1}}在整个应用程序中,我试图调用updateDisplayList() ... aggrrh ..我没有想法:))。

有什么想法吗?

任何帮助,像往常一样,非常感谢:)

1 个答案:

答案 0 :(得分:1)

如果您使用themeColorfocusTest.drawColor(true),则可以正常使用。你必须使用drawFocus()来重新着色,我不认为focusColor是Flex 3 setStyle属性(仅在Flex 4中使用)。

Tricky要发现,因为如果使用不正确的setStyle / getStyle属性,Flex不会抛出任何错误,它只会忽略它们!

    if (focusTest.getStyle('themeColor') != 0x008CEA) {
        focusTest.setStyle('themeColor', 0x008CEA);
    } else {
       focusTest.setStyle('themeColor', 0xFF0000);
    } 
   focusTest.drawFocus(true);

完整代码:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                creationComplete="init()">
    <mx:Script><![CDATA[    
        private function init():void {
            //putting focus inside needed component
            focusTest.setFocus();
            focusTest.setSelection(0, 0);

            // creates a new Timer
            var minuteTimer:Timer = new Timer(1000, 30);

            // designates listeners for the interval and completion events
            minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
            minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

            // starts the timer ticking
            minuteTimer.start();
        }    
        public function onTick(event:TimerEvent):void {
            // displays the tick count so far
            // displays the tick count so far

            trace("tick " + event.target.currentCount);
            if (focusTest.getStyle('themeColor') != 0x008CEA) {
                focusTest.setStyle('themeColor', 0x008CEA);
            } else {
                focusTest.setStyle('themeColor', 0xFF0000);
            }

            focusTest.drawFocus(true);
            //Update everything somehow :)             
        }    
        public function onTimerComplete(event:TimerEvent):void {
            trace("Time's Up!");
        }            
    ]]></mx:Script>
    <mx:TextInput id="focusTest" width="222"/>
</mx:Application>