2010-11-22 30 views
0

我在我的應用程序中使用TextArea,並希望在運行時根據特定條件更改textArea的字體顏色?我在函數中使用以下代碼如何在運行時更改textArea的顏色?

resultText.setStyle(「color」,0x842D22)。 但是,這不起作用,並在運行時給我一個例外 無法訪問空對象引用的屬性。

setStyle似乎不適用於textArea。請指教。

+1

你可以使用setStyle方法resultText對象的其他屬性?當您嘗試使用它時,null對象引用可能指示resultText爲null。也許你可以發佈你遇到問題的方法的一個更大的片段。 – martineno 2010-11-22 08:16:57

回答

2

看看這個例子可以幫助你:

<?xml version="1.0" encoding="utf-8"?> 
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
<mx:Script> 
    <![CDATA[ 
     public var flag:Boolean = true; 
     public function clickHandler():void{ 
     if(flag) 
      {resultText.setStyle("color","red"); 
      } 
      else 
      { 
      resultText.setStyle("color","blue"); 
      }  
      flag = !flag; 
     } 
     ]]> 
</mx:Script><mx:TextArea x="59" y="104" id="resultText"/> 
<mx:Button id="colorChangeButton" label="Change Color" click="clickHandler()" x="83" y="180" /></mx:Application> 

嘗試的情況下,這並不幫助共享代碼..

+0

此解決方案僅更改文本顏色。改爲在backgroundColor上設置樣式。 – adamcodes 2010-11-22 14:56:08

+0

@adam代碼..問的問題是設置字體顏色。總之,它不關心什麼屬性被設置。由於異常是'空對象引用',當setStyle完成時resultText實例以某種方式爲空。 – himanshu 2010-11-23 05:10:02

+0

你是對的 - 我不知道爲什麼我認爲bgcolor。抱歉。 – adamcodes 2010-11-23 14:29:13

相關問題