2011-03-23 26 views
1

我還有一個「我該如何」的問題:) 假設我有一個組件,並希望在運行時改變它的焦點顏色。下面是你(我已經排除任何按鈕等,以防止組件失去它的焦點,原因在這種情況下,它完全改變它的顏色)的例子:強制組件重畫它的焦點

<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」,我已經試過呼籲整個應用updateDisplayList()重繪,我已經試過打電話給styleChanged ... aggrrh ..我出於想法:))。

有什麼想法?

任何幫助,像往常一樣,非常感謝:)

+1

如果我猜「focusColor」是不是執行重繪對飛;所以你必須失去焦點並重新獲得焦點(聽起來就像你在切換瀏覽器標籤時所做的那樣),或者覆蓋updateDisplayList()以重繪焦點環,如果它改變的話。您將不得不通過框架代碼來確定繪製焦點元素的位置/時間/方式。 – JeffryHouser 2011-03-23 13:32:13

+0

Aww ..對'focusTest.drawFocus(false); focusTest.drawFocus(真);」解決了這個問題..我只是想知道還有沒有更好的解決方案? :) P.S.謝謝你的幫助:)你就像Super-Flex-man ..第一個在哪裏,並在正確的地方打*咯咯* – Antenka 2011-03-23 13:43:28

+0

很高興幫助。 ;) – JeffryHouser 2011-03-23 14:59:56

回答

1

如果使用themeColorfocusTest.drawColor(true)它工作正常。你必須使用drawFocus()來重新着色,我不認爲focusColor是一個Flex 3的setStyle屬性(僅在Flex 4中使用)。

棘手的問題,因爲如果你使用不正確的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> 
+0

* Sight *無法讓它工作..它甚至沒有更新,如果我切換一個選項卡,並返回。但無論如何,感謝您的嘗試:)順便說一句,我已經看到有人推薦在類似的情況下使用'themeColor',但我有一些懷疑..它正在改變全球顏色,如果其他人正在使用它呢? – Antenka 2011-03-23 14:37:55

+0

嗨。我將上面使用/測試過的完整代碼放在了上面,而且工作正常。嘗試一下,也許有一些細微的差別 – Ryan 2011-03-23 14:42:05

+0

這很奇怪,但它在這裏不工作.. :(完全你的代碼..我可以看到跟蹤,每秒鐘通知我有關更改,但在應用程序中沒有任何可視變化。 – Antenka 2011-03-23 14:54:39