2011-08-01 15 views
3

我想要一個工具提示,當鼠標移過組件的不同部分時,它會顯示不同的東西。例如,如果它是組件的上半部分,它將顯示一個工具提示。如果鼠標位於該段的下半部分,則工具提示將是另一個。我有一些我寫的代碼返回一個字符串中的面板。這段代碼是在另一臺計算機上,所以我明天會發布代碼。動作腳本中組件不同部分的不同工具提示

是否有可能在ActionScript中爲段的不同部分提供不同的工具提示(或工具提示中的不同值)?

我到目前爲止的代碼是:

MyToolTip.mxml

<?xml version="1.0"?> 
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" 
implements="mx.core.IToolTip" 
alpha=".9" width="325" borderColor="black" borderStyle="solid" 
cornerRadius="10" horizontalAlign="center"> 
<mx:Script><![CDATA[ 
[Bindable] 
public var toolTipText:String = ""; 

public var _text:String; 
[Bindable] 
public function get text():String { return _text; } 
public function set text(value:String):void {} 
]]></mx:Script> 

<mx:HBox width="100%" height="100%"> 
<mx:Text text = "Text here" width = "50%"/> 
<mx:Text text = "{toolTipText}" width = "50%"/> 
</mx:HBox> 
</mx:Panel> 

而且我想要的工具提示中對那麼我的動作腳本類成分。

public class MyComponent extends mx.containers.VBox { 

    private var tt:MyToolTip 

    public function MyComponent() { 
     this.addEventListener(ToolTipEvent.TOOL_TIP_CREATE, toolTipCreateHandler); 
     this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler); 
     tt = new MyToolTip(); 
    } 

    override protected function drawFigure():void { 
     //Need to kick the TOOL_TIP_CREATE event...and needs to be a value (eg a SPACE). 
     //If blank then no tooltip is created 
     this.toolTip = " "; 
     super.drawFigure(); 
    } 

    private function toolTipCreateHandler(event:ToolTipEvent):void { 
     var toolTipText:String = "tooltip1"; 
     eventToolTip.toolTipText = toolTipText; 
     event.toolTip = tt; 
    } 

    private function mouseOverHandler(event:MouseEvent):void { 
     //perhaps I need to be more efficient here and only fire 
     //when the mouse goes into top half or bottom half 
     //This does not appear to update the toolTipText in the view 
     var halfwayUp:Number = getBounds(this).height/2; 
     if (event.localY < halfwayUp) { 
      eventToolTip.toolTipText = "tooltip2"; 
     } 
     else { 
      eventToolTip.toolTipText = "tooltip1"; 
     }   
    } 
} 
} 

任何幫助或指示如何更新工具提示,當它已經顯示會很好。

+0

您是否使用任何組件框架(如flex或flash)? –

+0

下一頁:兩個工具提示有什麼不同?只是文字或顏色,形狀和其他東西? –

+0

工具提示位於閃存組件上。對不起,我不記得確切的組件,因爲我的代碼在另一臺計算機上。組件中的東西只是文本。我在面板上放置了一個文本列表,以便格式化得很好。這本質上是一堆鍵值對。鍵值對將根據鼠標懸停在組件上的位置而有所不同。 – RNJ

回答

2

是的,它的可能,訣竅在於知道工具提示是如何工作的:
如果您將鼠標懸停在組件上,並且在鼠標懸停時被銷燬,則會創建工具提示。因此,如果在顯示工具提示時更改文本,則不會看到更改,因爲set toolTip()函數不會創建新的工具提示(如果已存在)。因此,解決方案是銷燬當前顯示的工具提示,然後製作一個新提示。要銷燬工具提示,可以將其值設置爲空字符串。
下面是一個示例代碼:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application 
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
mouseMove="application1_mouseMoveHandler(event)"> 
<mx:Script> 
<![CDATA[ 
    import mx.managers.ToolTipManager; 

    protected function application1_mouseMoveHandler(event:MouseEvent):void{ 
     if (mouseX < 100) { 
      testButton.toolTip = "" 
      testButton.toolTip = "on the left side"; 
     } else { 
      testButton.toolTip = "" 
      testButton.toolTip = "on the right side"; 

     } 
    } 

]]> 
</mx:Script> 
<mx:Button id="testButton" label="test" width="200" height="200" /> 
</mx:Application> 

注意:如果你想要更多的混亂與Flex的提示,你可以用ToolTipManager.currentToolTip當前的提示(和不破壞它修改其屬性)。

+0

謝謝你。我感謝您的幫助! – RNJ

相關問題