【问题标题】:Different tooltips for different parts of a component in Action Script动作脚本中组件不同部分的不同工具提示
【发布时间】:2011-10-17 17:34:55
【问题描述】:

我想要一个工具提示,当鼠标移过组件的不同部分时显示不同的内容。例如,如果它是组件的上半部分,它将显示一个工具提示。如果鼠标位于段的下半部分,则工具提示将是另一个。我写了一些代码,它返回一个带有字符串的面板。这个代码在另一台计算机上,所以我明天会发布代码。

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";
        }           
    }
}
}

任何关于如何在工具提示已显示时更新它的帮助或指示都会很棒。

【问题讨论】:

  • 你使用flex或flash等组件框架吗?
  • 下一步:两个工具提示有什么不同?只是文字或颜色、形状和其他内容?
  • 工具提示位于 Flash 组件上。抱歉,我不记得确切的组件,因为我的代码在另一台计算机上。组件中的内容只是文本。我在面板上放置了一个文本列表,以便它的格式很好。它本质上是一堆键值对。键值对将根据鼠标悬停在组件上的位置而有所不同。
  • 我希望能够使用面板中可绑定的字段来执行此操作。然后我可以根据鼠标的位置更新字段,然后工具提示会更新。我第一次尝试这个并没有奏效。我不认为工具提示在改变,即使后面的代码随着鼠标的移动而改变。
  • 您不能只为需要不同工具提示的每个部分提供正确的工具提示吗?如果您发布显示您的文本列表的代码,我(或某人)也许可以帮助您提供特定代码。

标签: apache-flex actionscript-3 actionscript tooltip mxml


【解决方案1】:

是的,有可能,诀窍是了解工具提示的工作原理:
如果您将鼠标悬停在组件上,则会创建工具提示,如果您将鼠标移出,则会将其销毁。因此,如果您在显示时更改工具提示上的文本,那么您不会看到更改,因为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 获取当前工具提示(并修改其属性而不破坏它)。

【讨论】:

  • 谢谢。感谢您的帮助!
猜你喜欢
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多