flex ToolTip汇总

flex ToolTip汇总
  • 博客分类:
  • Flex
    flex ToolTip

1.toolTip换行符
<mx:Button id="btn1" toolTip="在工具条提示中&#13; 使用换行符"/>

2.设定工具条提示的样式
As代码 复制代码 收藏代码
  1. <mx:Style> 
  2.     ToolTip 
  3.     { 
  4.         fontSize:19
  5.         color:#FF6699; 
  6.     } 
  7. </mx:Style> 
<mx:Style>
	ToolTip
	{
		fontSize:19;
		color:#FF6699;
	}
</mx:Style>


3.设定工具提示宽度
As代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"
  2. <mx:Script> 
  3.     <![CDATA[ 
  4.         import mx.controls.ToolTip; 
  5.         public function init():void 
  6.         { 
  7.             ToolTip.maxWidth=100
  8.         } 
  9.         ]]> 
  10. </mx:Script> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
	<![CDATA[
		import mx.controls.ToolTip;
		public function init():void
		{
			ToolTip.maxWidth=100;
		}
		]]>
</mx:Script>


3.使用工具条提示事件
toolTipCrate
toolTipEnd
toolTipHide
toolTipShow
toolTipShown
toolTipStart

As代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"
  2. <mx:Script> 
  3.     <![CDATA[ 
  4.         import mx.events.ToolTipEvent; 
  5.         import flash.media.Sound; 
  6.         [Embed(source="demo.mp3")] 
  7.         private var beepSound:Class; 
  8.         private var myClip:Sound; 
  9.         public function init():void 
  10.         { 
  11.             TooTip.maxWidth=100
  12.         } 
  13.  
  14.         public function playSoune():void 
  15.         { 
  16.             myClip.play(); 
  17.         } 
  18.  
  19.         public function myListener(event:ToolTipEvent):void 
  20.         { 
  21.             playSoune(); 
  22.         } 
  23.          
  24.         public function init():void 
  25.         { 
  26.             lbl.addEventListener(ToolTipEvent.TOOL_TIP_SHOW,myListener); 
  27.             myClip=new beepSound(); 
  28.         } 
  29.         ]]> 
  30. </mx:Script> 
  31. <mx:Label id="lbl" toolTip="demo ToolTip" text="mouse over here" /> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
	<![CDATA[
		import mx.events.ToolTipEvent;
		import flash.media.Sound;
		[Embed(source="demo.mp3")]
		private var beepSound:Class;
		private var myClip:Sound;
		public function init():void
		{
			TooTip.maxWidth=100;
		}

		public function playSoune():void
		{
			myClip.play();
		}

		public function myListener(event:ToolTipEvent):void
		{
			playSoune();
		}
		
		public function init():void
		{
			lbl.addEventListener(ToolTipEvent.TOOL_TIP_SHOW,myListener);
			myClip=new beepSound();
		}
		]]>
</mx:Script>
<mx:Label id="lbl" toolTip="demo ToolTip" text="mouse over here" />


4.ToolTip Manager
ToolTip Manager 类用来管理toolTip属性,包含在mx.managers包中
使用ToolTip Manager 延迟工具条显示的时间,在工具条上使用行为效果
建立动态的工具条提示,使用自定义的工具条提示

公共属性
    currentTarget : DisplayObject
当前显示工具提示的 UIComponent;如果未显示工具提示,则为 null。 ToolTipManager
    currentToolTip : IToolTip
当前可见的 ToolTip 对象;如果未显示 ToolTip 对象,则为 null。 ToolTipManager
    enabled : Boolean
如果为 true,则当用户将鼠标指针移至组件上方时,ToolTipManager 会自动显示工具提示。 ToolTipManager
    hideDelay : Number
自工具提示出现时起,Flex 要隐藏此提示前所需等待的时间量(以毫秒为单位)。 ToolTipManager
    hideEffect : IAbstractEffect
隐藏工具提示时播放的效果;如果工具提示在消失时不应显示任何效果,则为 null。 ToolTipManager
   prototype : Object
对类或函数对象的原型对象的引用。 Object
    scrubDelay : Number
在 Flex 再次等待 showDelay 持续显示工具提示之前,用户在各控件间移动鼠标时可以花费的时间(以毫秒为单位)。 ToolTipManager
    showDelay : Number
当用户将鼠标移至具有工具提示的组件上方时,Flex 等待 ToolTip 框出现所需的时间(以毫秒为单位)。 ToolTipManager
    showEffect : IAbstractEffect
显示工具提示时播放的效果;如果工具提示在显示时不应显示任何效果,则为 null。 ToolTipManager
    toolTipClass : Class
创建工具提示要用到的类。



1.控制工具条的显示隐藏
ToolTip Manager  enabled属性来定义工具条提示状态,默认为true,设定为false就表示禁用
2.延迟工具条的提示时间
showDelay 默认为500毫秒 当鼠标在控件之间移动时,flex等待显示工具条提示的时间长度
hideDelay 默认值为10000毫秒 如果设定hideDelay属性值为0,flex不显示工具条提示,如果设定为Infinity,flex则一致显示工具提示
直到下一个动作,或者鼠标离开主键
例如:
ToolTipManager.showDelay=0;//立即显示
ToolTipManager.hideDelay=3000;//3s秒后隐藏
3.在工具条上使用动画效果
As代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"
  2. <mx:Script> 
  3.     <![CDATA[ 
  4.         import mx.managers.ToolTipManager; 
  5.         import mx.events.ToolTipEvent; 
  6.         import flash.media.Sound; 
  7.         [Embed(source="demo.mp3")] 
  8.         private var beepSound:Class; 
  9.         private var myClip:Sound; 
  10.         public function init():void 
  11.         { 
  12.             TooTip.maxWidth=100
  13.             ToolTipManager.showEffect=fadeIn; 
  14.         } 
  15.         ]]> 
  16. </mx:Script> 
  17. <mx:Label id="lbl" toolTip="demo ToolTip" text="mouse over here" /> 
  18. <mx:Fade id="fadeIn" alphaFrom="0" alphaTo="1" duration="1000" /> 
  19. <mx:Panel > 
  20.     <mx:Button id="btn" toolTip="demoasdf" /> 
  21. </mx:Panel > 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
	<![CDATA[
		import mx.managers.ToolTipManager;
		import mx.events.ToolTipEvent;
		import flash.media.Sound;
		[Embed(source="demo.mp3")]
		private var beepSound:Class;
		private var myClip:Sound;
		public function init():void
		{
			TooTip.maxWidth=100;
			ToolTipManager.showEffect=fadeIn;
		}
		]]>
</mx:Script>
<mx:Label id="lbl" toolTip="demo ToolTip" text="mouse over here" />
<mx:Fade id="fadeIn" alphaFrom="0" alphaTo="1" duration="1000" />
<mx:Panel >
	<mx:Button id="btn" toolTip="demoasdf" />
</mx:Panel >


4.建立动态工具条体提示
ToolTipManager有两种方法,即:createToolTip()和destroyToolTip()
建立一个工具条提示对象时,可以定义其属性,样式,事件和效果。
crateToolTip()方法结构
createToolTip(text:String, x:Number, y:Number, errorTipBorderStyle:String = null, context:IUIComponent = null):IToolTip
[] 使用指定的文本创建 ToolTip 类的实例,然后在舞台坐标中的指定位置显示此实例。 ToolTipManager
    destroyToolTip(toolTip:IToolTip):void
[] 销毁由 createToolTip() 方法创建的指定 ToolTip。
例如:
<mx:Label id="lbl" toolTip="demo ToolTip" text="mouse over here" />
<mx:Button id="btn1" toolTip="填写的是{lbl.text}"/>

5.建立用户自定义的工具条提示

As代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  2. <mx:Script> 
  3.     <![CDATA[ 
  4.         import mx.managers.ToolTipManager; 
  5.         import mx.events.ToolTip; 
  6.         public var myTip:ToolTip; 
  7.  
  8.         public function init():void 
  9.         { 
  10.             var s:String ="保存,应用,退出"
  11.             myTip=ToolTipManager.createToolTip(s,10,10) as ToolTip; 
  12.             myTip.setStyle("backgroundColor",0xFFCC00); 
  13.             myTip.width=300
  14.             myTip.height=40
  15.         } 
  16.  
  17.         public funciton destroyBigTip():void 
  18.         { 
  19.             ToolTipManager.destroyToolTip(myTip); 
  20.         } 
  21.         ]]> 
  22. </mx:Script> 
  23.     <mx:Button id="btn" toolTip="test toolTip" rollOver="init()" rollOut="destroyBigTip()"/> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
	<![CDATA[
		import mx.managers.ToolTipManager;
		import mx.events.ToolTip;
		public var myTip:ToolTip;

		public function init():void
		{
			var s:String ="保存,应用,退出";
			myTip=ToolTipManager.createToolTip(s,10,10) as ToolTip;
			myTip.setStyle("backgroundColor",0xFFCC00);
			myTip.width=300;
			myTip.height=40;
		}

		public funciton destroyBigTip():void
		{
			ToolTipManager.destroyToolTip(myTip);
		}
		]]>
</mx:Script>
	<mx:Button id="btn" toolTip="test toolTip" rollOver="init()" rollOut="destroyBigTip()"/>


6.使用错误信息提示工具条
As代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  2. <mx:Script> 
  3.     <![CDATA[ 
  4.         import mx.managers.ToolTipManager; 
  5.         import mx.events.ToolTip; 
  6.         public var errorTip:ToolTip; 
  7.     private function validatefun(type:String,event:Object):void 
  8.     { 
  9.         errorTip=" demo test "
  10.         errorTip=ToolTipManager.createToolTip(errorTip,event.currentTarget.x,event.currentTarget.y) as ToolTip; 
  11.         errorTip.setStyle("styleName","errorTip"); 
  12.     } 
  13. <mx:TextInput id="txt" enter="validatefun('demo',event)" /> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
	<![CDATA[
		import mx.managers.ToolTipManager;
		import mx.events.ToolTip;
		public var errorTip:ToolTip;
	private function validatefun(type:String,event:Object):void
	{
		errorTip=" demo test ";
		errorTip=ToolTipManager.createToolTip(errorTip,event.currentTarget.x,event.currentTarget.y) as ToolTip;
		errorTip.setStyle("styleName","errorTip");
	}
<mx:TextInput id="txt" enter="validatefun('demo',event)" />


7.重写工具条皮肤
ToolTip皮肤存在mx.skins.ToolTipBorder.as文件中
当然需要重写的时候就可以 ToolTipBorder 类继承的类就可以了
例如:
Java代码 复制代码 收藏代码
  1. package 
  2.     import mx.skins.RectangularBorder; 
  3.  
  4.     public class MyToolTipBorder extends RectangularBorder 
  5.     { 
  6.         public function MyToolTipBorder() 
  7.         { 
  8.             super(); 
  9.         } 
  10.          
  11.         override protected function updateDisplayList(unscaledWidth:Number, 
  12.                                                   unscaledHeight:Number):void 
  13.         { 
  14.              
  15.         } 
  16.          
  17.          override   public function layoutBackgroundImage():void
  18.                  
  19.             } 
  20.     } 
  21. }mxml中使用 
  22. <mx:Style> 
  23.     ToolTip 
  24.     { 
  25.         borderSkin:ClassReference("MyToolTipBorder"); 
  26.     } 
  27. </mx:Style> 
package
{
	import mx.skins.RectangularBorder;

	public class MyToolTipBorder extends RectangularBorder
	{
		public function MyToolTipBorder()
		{
			super();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,
                                                  unscaledHeight:Number):void
	    {
	    	
	    }
	    
	     override   public function layoutBackgroundImage():void {
	        	
	        }
	}
}mxml中使用
<mx:Style>
	ToolTip
	{
		borderSkin:ClassReference("MyToolTipBorder");
	}
</mx:Style>



自定义皮肤 参见:http://demojava.iteye.com/blog/1175446
自定义DateGird Item ToolTip
效果图:

自定义ToolTip

renderers.MenuToolTipRenderer as 实现接口   IMenuItemRenderer, IDataRenderer, IListItemRenderer
Xml代码 复制代码 收藏代码
  1. package renderers 
  2. import flash.events.Event; 
  3. import flash.events.MouseEvent; 
  4. import flash.text.TextFieldAutoSize; 
  5.  
  6. import mx.controls.Alert; 
  7. import mx.controls.LinkButton; 
  8. import mx.controls.Menu; 
  9. import mx.controls.listClasses.IListItemRenderer; 
  10. import mx.controls.menuClasses.IMenuItemRenderer; 
  11. import mx.core.EdgeMetrics; 
  12. import mx.core.IDataRenderer; 
  13. import mx.core.UIComponent; 
  14. import mx.core.UITextField; 
  15. import mx.events.FlexEvent; 
  16.  
  17.  
  18. public class MenuToolTipRenderer extends UIComponent implements IMenuItemRenderer, IDataRenderer, IListItemRenderer 
  19.      
  20.  
  21.     private var textField:UITextField; 
  22.     private var clickText:LinkButton; 
  23.     public function get measuredTypeIconWidth():Number//类型图标(单选/复选)的宽度  
  24.     { 
  25.         return 33; 
  26.     } 
  27.     public function get measuredBranchIconWidth():Number//分支图标的宽度  
  28.     { 
  29.         return 33; 
  30.     } 
  31.     public function get measuredIconWidth():Number//图标的宽度 
  32.     { 
  33.         return 33; 
  34.     } 
  35.  
  36.      
  37.     //------------------------------------------------------------------------- 
  38.      
  39.     public function MenuToolTipRenderer() 
  40.     { 
  41.         super(); 
  42.         this.addEventListener(FlexEvent.DATA_CHANGE, renderComponent); 
  43.         this.setStyle("cornerRadius", 5); 
  44.         this.maxWidth = 250
  45.  
  46.     } 
  47.      
  48.     //------------------------------------------------------------------------- 
  49.      
  50.     // Internal variable for the property value. 
  51.     private var _menu:Menu; 
  52.  
  53.     public function get menu():Menu 
  54.     { 
  55.         return _menu; 
  56.     } 
  57.  
  58.     public function set menu(value:Menu):void 
  59.     { 
  60.         _menu = value
  61.     } 
  62.      
  63.     //------------------------------------------------------------------------- 
  64.      
  65.  
  66.     private var _data:Object; 
  67.      
  68.  
  69.     [Bindable("dataChange")] 
  70.     public function get data():Object { 
  71.     return _data; 
  72.     } 
  73.  
  74.     public function set data(value:Object):void { 
  75.     _data = value
  76.      
  77.     dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE)); 
  78.     } 
  79.      
  80.     //------------------------------------------------------------------------- 
  81.      
  82.     private function get borderMetrics():EdgeMetrics 
  83.     { 
  84.         return EdgeMetrics.EMPTY; 
  85.     } 
  86.      
  87.     //------------------------------------------------------------------------- 
  88.      
  89.     private function renderComponent(event:FlexEvent):void 
  90.     { 
  91.         if(_data != null && _data != "null") 
  92.         { 
  93.             textField.htmlText = data.label; 
  94.             invalidateProperties(); 
  95.             invalidateSize(); 
  96.             invalidateDisplayList(); 
  97.              
  98.         } 
  99.     } 
  100.      
  101.     //------------------------------------------------------------------------- 
  102.      
  103.     private function buttonClick(event:Event):void 
  104.     { 
  105.         mx.controls.Alert.show("button click"); 
  106.     } 
  107.      
  108.     //------------------------------------------------------------------------- 
  109.      
  110.     override protected function createChildren():void 
  111.     { 
  112.         super.createChildren(); 
  113.  
  114.         // Create the TextField that displays the tooltip text. 
  115.         if (!textField) 
  116.         { 
  117.             textField = new UITextField(); 
  118.             textField.autoSize = TextFieldAutoSize.LEFT; 
  119.             textField.mouseEnabled = false
  120.             textField.multiline = true
  121.             textField.selectable = false
  122.             textField.wordWrap = true
  123.             textField.styleName = this
  124.             addChild(textField);     
  125.         } 
  126.     } 
  127.      
  128.     //------------------------------------------------------------------------- 
  129.      
  130.      
  131.     override protected function measure():void 
  132.     { 
  133.         super.measure(); 
  134.  
  135.         var bm:EdgeMetrics = borderMetrics
  136.  
  137.         var leftInset:Number = bm.left + getStyle("paddingLeft"); 
  138.         var topInset:Number = bm.top + getStyle("paddingTop"); 
  139.         var rightInset:Number = bm.right + getStyle("paddingRight"); 
  140.         var bottomInset:Number = bm.bottom + getStyle("paddingBottom"); 
  141.  
  142.         var widthSlop:Number = leftInset + rightInset; 
  143.         var heightSlop:Number = topInset + bottomInset; 
  144.  
  145.         textField.wordWrap = false
  146.  
  147.         if (textField.textWidth + widthSlop > this.maxWidth) 
  148.         { 
  149.             textField.width = this.maxWidth - widthSlop; 
  150.             textField.wordWrap = true
  151.         } 
  152.  
  153.         measuredWidth = textField.width + widthSlop; 
  154.         measuredHeight = textField.height + heightSlop; 
  155.          
  156.         thisthis.parent.parent.height = this.parent.height = this.height = measuredHeight + 25; 
  157.         thisthis.parent.parent.width = this.parent.width = this.width = measuredWidth
  158.          
  159.         createAdditionalContent(measuredWidth, measuredHeight); 
  160.     } 
  161.      
  162.     private function createAdditionalContent(measuredWidth:Number, measuredHeight:Number):void 
  163.     { 
  164.         clickText = new LinkButton(); 
  165.         clickText.label = "More Info"
  166.         clickText.width = 70
  167.         clickText.height = 18
  168.         clickText.setStyle("fontSize", 9); 
  169.         clickText.setStyle("color", "blue"); 
  170.         clickText.buttonMode = true
  171.         clickText.visible = true
  172.         clickText.y = measuredHeight
  173.         clickText.x = measuredWidth - clickText.width - 8; 
  174.          
  175.         clickText.addEventListener(MouseEvent.CLICK, dispatchClick); 
  176.  
  177.         addChild(clickText); 
  178.     } 
  179.      
  180.     private function dispatchClick(e:Event):void 
  181.     { 
  182.         this.dispatchEvent(new Event("buttonClicked", true)) 
  183.     } 
package renderers
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextFieldAutoSize;

import mx.controls.Alert;
import mx.controls.LinkButton;
import mx.controls.Menu;
import mx.controls.listClasses.IListItemRenderer;
import mx.controls.menuClasses.IMenuItemRenderer;
import mx.core.EdgeMetrics;
import mx.core.IDataRenderer;
import mx.core.UIComponent;
import mx.core.UITextField;
import mx.events.FlexEvent;


public class MenuToolTipRenderer extends UIComponent implements IMenuItemRenderer, IDataRenderer, IListItemRenderer
{
	

	private var textField:UITextField;
	private var clickText:LinkButton;
	public function get measuredTypeIconWidth():Number//类型图标(单选/复选)的宽度 
	{
		return 33;
	}
	public function get measuredBranchIconWidth():Number//分支图标的宽度 
	{
		return 33;
	}
	public function get measuredIconWidth():Number//图标的宽度
	{
		return 33;
	}

	
	//-------------------------------------------------------------------------
	
	public function MenuToolTipRenderer()
	{
		super();
		this.addEventListener(FlexEvent.DATA_CHANGE, renderComponent);
		this.setStyle("cornerRadius", 5);
		this.maxWidth = 250;

	}
	
	//-------------------------------------------------------------------------
	
	// Internal variable for the property value.
	private var _menu:Menu;

	public function get menu():Menu
	{
		return _menu;
	}

	public function set menu(value:Menu):void
	{
		_menu = value;
	}
	
	//-------------------------------------------------------------------------
	

    private var _data:Object;
    

    [Bindable("dataChange")]
    public function get data():Object {
	return _data;
    }

    public function set data(value:Object):void {
	_data = value;
    
	dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
    }
    
    //-------------------------------------------------------------------------
	
	private function get borderMetrics():EdgeMetrics
	{
		return EdgeMetrics.EMPTY;
	}
	
	//-------------------------------------------------------------------------
	
	private function renderComponent(event:FlexEvent):void
	{
		if(_data != null && _data != "null")
		{
			textField.htmlText = data.label;
			invalidateProperties();
			invalidateSize();
			invalidateDisplayList();
			
		}
	}
	
	//-------------------------------------------------------------------------
	
	private function buttonClick(event:Event):void
	{
		mx.controls.Alert.show("button click");
	}
	
	//-------------------------------------------------------------------------
	
	override protected function createChildren():void
	{
		super.createChildren();

		// Create the TextField that displays the tooltip text.
		if (!textField)
		{
			textField = new UITextField();
			textField.autoSize = TextFieldAutoSize.LEFT;
			textField.mouseEnabled = false;
			textField.multiline = true;
			textField.selectable = false;
			textField.wordWrap = true;
			textField.styleName = this;
			addChild(textField);	
		}
	}
	
	//-------------------------------------------------------------------------
	
	
	override protected function measure():void
	{
		super.measure();

		var bm:EdgeMetrics = borderMetrics;

		var leftInset:Number = bm.left + getStyle("paddingLeft");
		var topInset:Number = bm.top + getStyle("paddingTop");
		var rightInset:Number = bm.right + getStyle("paddingRight");
		var bottomInset:Number = bm.bottom + getStyle("paddingBottom");

		var widthSlop:Number = leftInset + rightInset;
		var heightSlop:Number = topInset + bottomInset;

		textField.wordWrap = false;

		if (textField.textWidth + widthSlop > this.maxWidth)
		{
			textField.width = this.maxWidth - widthSlop;
			textField.wordWrap = true;
		}

		measuredWidth = textField.width + widthSlop;
		measuredHeight = textField.height + heightSlop;
		
		this.parent.parent.height = this.parent.height = this.height = measuredHeight + 25;
		this.parent.parent.width = this.parent.width = this.width = measuredWidth;
		
		createAdditionalContent(measuredWidth, measuredHeight);
	}
	
	private function createAdditionalContent(measuredWidth:Number, measuredHeight:Number):void
	{
		clickText = new LinkButton();
		clickText.label = "More Info";
		clickText.width = 70;
		clickText.height = 18;
		clickText.setStyle("fontSize", 9);
		clickText.setStyle("color", "blue");
		clickText.buttonMode = true;
		clickText.visible = true;
		clickText.y = measuredHeight;
		clickText.x = measuredWidth - clickText.width - 8;
		
		clickText.addEventListener(MouseEvent.CLICK, dispatchClick);

		addChild(clickText);
	}
	
	private function dispatchClick(e:Event):void
	{
		this.dispatchEvent(new Event("buttonClicked", true))
	}
}
}


Application.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()"  
  3.             viewSourceURL="srcview/index.html" width="600" height="300"> 
  4.  
  5. <mx:DataGrid id="dg" dataProvider="{tmpArray}" width="550" 
  6.              mouseOut="dgMouseOut(event)" mouseOver="dgMouseOver(event)" mouseMove="dgMouseMove(event)"> 
  7.     <mx:columns> 
  8.         <mx:Array> 
  9.             <mx:DataGridColumn headerText="Label" dataField="label" showDataTips="true" /> 
  10.             <mx:DataGridColumn headerText="Data" dataField="data" /> 
  11.         </mx:Array> 
  12.     </mx:columns> 
  13. </mx:DataGrid> 
  14.  
  15. <mx:Style> 
  16.      
  17.     .menuToolTip { 
  18.            backgroundAlpha: 1; 
  19.            backgroundColor: #ffffcc; 
  20.            useRollOver: false; 
  21.            borderStyle: solid; 
  22.            borderThickness: 0; 
  23.            borderColor: #000000; 
  24.            selectionColor: #7fcdfe; 
  25.            dropShadowEnabled: true; 
  26.            cornerRadius: 5; 
  27.            paddingLeft: 3; paddingRight: 3; paddingTop: 3; paddingBottom: 3; 
  28.             
  29.         } 
  30.      
  31. </mx:Style> 
  32.  
  33. <mx:Script> 
  34.     <![CDATA[
  35.         import mx.automation.delegates.controls.ListBaseContentHolderAutomationImpl;
  36.         import mx.controls.listClasses.ListBase;
  37.         import renderers.MenuToolTipRenderer;
  38.         import mx.controls.dataGridClasses.DataGridItemRenderer;
  39.        
  40.         import mx.managers.ToolTipManager;
  41.         import mx.controls.Menu;
  42.         import mx.events.MenuEvent;
  43.         import flash.geom.Point;
  44.        
  45.         [Bindable] private var dgMouseX:Number = 0;
  46.         [Bindable] private var dgMouseY:Number = 0;
  47.        
  48.         private var toolTipMenu:Menu;
  49.         private var toolTipTimer:Timer;
  50.        
  51.         [Bindable]
  52.         public var tmpArray:Array = [{label: '<b>mary</b> had a little lamb', data: 55, link:'http://www.meutzner.com/blog'},
  53.                                      {label: 'All the kings men, and all the kings horses', data: 56, link:'http://www.meutzner.com/blog'},
  54.                                      {label: 'baa baa black sheep, have you any wool', data: 57, link:'http://www.meutzner.com/blog'},
  55.                                      {label: 'jack and jill went up the hill', data: 58, link:'http://www.meutzner.com/blog'},
  56.                                      {label: 'little miss muffet, sat on a tuffet, eating her curds and whey', data: 59, link:'http://www.meutzner.com/blog'}
  57.                                     ];
  58.        
  59.         //-------------------------------------------------------------------------
  60.        
  61.         public function init():void
  62.         {
  63.             ToolTipManager.enabled = false;
  64.         }
  65.        
  66.         //-------------------------------------------------------------------------
  67.        
  68.         private function createMenu(data:Object):void
  69.     {
  70.     toolTipTimer.stop();
  71.     var menuData:Array = [{label: data.label, link: data.link}];
  72.    
  73.    
  74.    
  75.     toolTipMenu = Menu.createMenu(this, menuData, false);
  76.     toolTipMenu.labelField="label";
  77.     toolTipMenu.width = 250;
  78.     toolTipMenu.setStyle("fontSize", 10);
  79.     toolTipMenu.setStyle("themeColor", 0xFF9900);
  80.     toolTipMenu.itemRenderer = new ClassFactory(renderers.MenuToolTipRenderer);
  81.     toolTipMenu.data = data;
  82.     toolTipMenu.selectable = false;
  83.     toolTipMenu.setStyle("styleName", "menuToolTip");
  84.     toolTipMenu.setStyle("openDuration", 0);
  85.     //toolTipMenu.setStyle("selectionEasingFunction", myEasingFunction);
  86.     toolTipMenu.addEventListener(MouseEvent.ROLL_OUT, hideToolTip);
  87.     toolTipMenu.addEventListener("buttonClicked", catchButtonClick);
  88.    
  89.     var pt1:Point = new Point(this.dgMouseX, this.dgMouseY);
  90.             pt1 = dg.localToGlobal(pt1);
  91.             //offset this for now to fix slight bug where immediate mouseover of tooltip causes it to hide
  92.     toolTipMenu.show(pt1.x+10, pt1.y+0);
  93.    
  94.     }
  95.    
  96.     //-------------------------------------------------------------------------
  97.    
  98.     private function hideToolTip(event:MouseEvent):void
  99.     {
  100.     //trace("MENU --- " + event.relatedObject + " - " + event.target + " - " + event.currentTarget);
  101.     toolTipMenu.hide();
  102.     }
  103.    
  104.     //-------------------------------------------------------------------------
  105.    
  106.     private function dgMouseMove(event:MouseEvent):void
  107.     {
  108.     this.dgMouseX = dg.mouseX;
  109.     this.dgMouseY = dg.mouseY;
  110.     }
  111.    
  112.    
  113.     //-------------------------------------------------------------------------
  114.    
  115.     private function dgMouseOver(event:MouseEvent):void
  116.     {
  117.     this.dgMouseX = dg.mouseX;
  118.     this.dgMouseY = dg.mouseY;
  119.     //trace("DATAGRID OVER --- " + event.target + " - " + event.currentTarget + " - " + event.relatedObject);
  120.     if(event.target is DataGridItemRenderer)
  121.     {
  122.         if(toolTipMenu != null)
  123.         {
  124.             if(toolTipMenu.visible && toolTipMenu.data == event.target.data)
  125.                 return;
  126.         }
  127.         if(!DataGridItemRenderer(event.target).styleName.showDataTips)
  128.             return;
  129.            
  130.         toolTipTimer = new Timer(1000, 0);
  131.         toolTipTimer.addEventListener(TimerEvent.TIMER, function():void
  132.                                                                  {
  133.                                                                     createMenu(event.target.data);
  134.                                                                  }
  135.                                       );
  136.         toolTipTimer.start();
  137.     }
  138.     }
  139.    
  140.     //-------------------------------------------------------------------------
  141.    
  142.     private function dgMouseOut(event:MouseEvent):void
  143.     {
  144.     if(toolTipTimer) toolTipTimer.stop();
  145.     //trace("DATAGRID OUT --- " + event.target + " - " + event.currentTarget + " - " + event.relatedObject);
  146.     if(event.relatedObject is mx.controls.listClasses.ListBaseContentHolder || event.relatedObject is MenuToolTipRenderer)
  147.     {
  148.         return;
  149.     }
  150.     if(event.target is DataGridItemRenderer)
  151.     {
  152.         if(event.relatedObject is Menu || !event.relatedObject is MenuToolTipRenderer) return;
  153.         if(toolTipMenu != null) toolTipMenu.hide();
  154.         }
  155.     }
  156.    
  157.     //-------------------------------------------------------------------------
  158.    
  159.     private function catchButtonClick(e:Event):void
  160.         {
  161.     mx.controls.Alert.show(e.target.data.label + " was clicked to take you to " + e.target.data.link);
  162.     }
  163.        
  164.     ]]> 
  165. </mx:Script> 
  166.  
  167. </mx:Application> 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" 
			viewSourceURL="srcview/index.html" width="600" height="300">

<mx:DataGrid id="dg" dataProvider="{tmpArray}" width="550"
			 mouseOut="dgMouseOut(event)" mouseOver="dgMouseOver(event)" mouseMove="dgMouseMove(event)">
	<mx:columns>
		<mx:Array>
			<mx:DataGridColumn headerText="Label" dataField="label" showDataTips="true" />
			<mx:DataGridColumn headerText="Data" dataField="data" />
		</mx:Array>
	</mx:columns>
</mx:DataGrid>

<mx:Style>
	
	.menuToolTip {
		   backgroundAlpha: 1;
		   backgroundColor: #ffffcc;
		   useRollOver: false;
		   borderStyle: solid;
		   borderThickness: 0;
		   borderColor: #000000;
		   selectionColor: #7fcdfe;
		   dropShadowEnabled: true;
		   cornerRadius: 5;
		   paddingLeft: 3; paddingRight: 3; paddingTop: 3; paddingBottom: 3;
		   
		}
	
</mx:Style>

<mx:Script>
	<![CDATA[
		import mx.automation.delegates.controls.ListBaseContentHolderAutomationImpl;
		import mx.controls.listClasses.ListBase;
		import renderers.MenuToolTipRenderer;
		import mx.controls.dataGridClasses.DataGridItemRenderer;
		
		import mx.managers.ToolTipManager;
		import mx.controls.Menu;
		import mx.events.MenuEvent;
		import flash.geom.Point;
		
		[Bindable] private var dgMouseX:Number = 0;
		[Bindable] private var dgMouseY:Number = 0;
		
		private var toolTipMenu:Menu;
		private var toolTipTimer:Timer;
		
		[Bindable]
		public var tmpArray:Array = [{label: '<b>mary</b> had a little lamb', data: 55, link:'http://www.meutzner.com/blog'}, 
								     {label: 'All the kings men, and all the kings horses', data: 56, link:'http://www.meutzner.com/blog'}, 
								     {label: 'baa baa black sheep, have you any wool', data: 57, link:'http://www.meutzner.com/blog'}, 
								     {label: 'jack and jill went up the hill', data: 58, link:'http://www.meutzner.com/blog'}, 
								     {label: 'little miss muffet, sat on a tuffet, eating her curds and whey', data: 59, link:'http://www.meutzner.com/blog'}
								    ];
		
		//-------------------------------------------------------------------------
		
		public function init():void
		{
			ToolTipManager.enabled = false;
		}
		
		//-------------------------------------------------------------------------
		
		private function createMenu(data:Object):void 
    {
	toolTipTimer.stop();
	var menuData:Array = [{label: data.label, link: data.link}];
	
	
	
	toolTipMenu = Menu.createMenu(this, menuData, false);
	toolTipMenu.labelField="label";
	toolTipMenu.width = 250;
	toolTipMenu.setStyle("fontSize", 10);
	toolTipMenu.setStyle("themeColor", 0xFF9900);
	toolTipMenu.itemRenderer = new ClassFactory(renderers.MenuToolTipRenderer);
	toolTipMenu.data = data;
	toolTipMenu.selectable = false;
	toolTipMenu.setStyle("styleName", "menuToolTip");
	toolTipMenu.setStyle("openDuration", 0);

	//toolTipMenu.setStyle("selectionEasingFunction", myEasingFunction);
	toolTipMenu.addEventListener(MouseEvent.ROLL_OUT, hideToolTip);
	toolTipMenu.addEventListener("buttonClicked", catchButtonClick);
	
	var pt1:Point = new Point(this.dgMouseX, this.dgMouseY);
			pt1 = dg.localToGlobal(pt1);
			//offset this for now to fix slight bug where immediate mouseover of tooltip causes it to hide
	toolTipMenu.show(pt1.x+10, pt1.y+0);
	
    }
    
    //-------------------------------------------------------------------------
    
    private function hideToolTip(event:MouseEvent):void
    {
	//trace("MENU --- " + event.relatedObject + " - " + event.target + " - " + event.currentTarget);
	toolTipMenu.hide();
    }
    
    //-------------------------------------------------------------------------
    
    private function dgMouseMove(event:MouseEvent):void
    {
	this.dgMouseX = dg.mouseX;
	this.dgMouseY = dg.mouseY;
    }
    
    
    //-------------------------------------------------------------------------
    
    private function dgMouseOver(event:MouseEvent):void
    {
	this.dgMouseX = dg.mouseX;
	this.dgMouseY = dg.mouseY;
	//trace("DATAGRID OVER --- " + event.target + " - " + event.currentTarget + " - " + event.relatedObject);
	if(event.target is DataGridItemRenderer)
	{
		if(toolTipMenu != null)
		{
			if(toolTipMenu.visible && toolTipMenu.data == event.target.data) 
				return;
		}
		if(!DataGridItemRenderer(event.target).styleName.showDataTips)
			return;
			
		toolTipTimer = new Timer(1000, 0);
		toolTipTimer.addEventListener(TimerEvent.TIMER, function():void 
																 { 
																	createMenu(event.target.data);
																 } 
									  );
		toolTipTimer.start();
	}
    }
    
    //-------------------------------------------------------------------------
    
    private function dgMouseOut(event:MouseEvent):void
    {
	if(toolTipTimer) toolTipTimer.stop();
	//trace("DATAGRID OUT --- " + event.target + " - " + event.currentTarget + " - " + event.relatedObject);
	if(event.relatedObject is mx.controls.listClasses.ListBaseContentHolder || event.relatedObject is MenuToolTipRenderer)
	{
		return;
	}
	if(event.target is DataGridItemRenderer)
	{
		if(event.relatedObject is Menu || !event.relatedObject is MenuToolTipRenderer) return;
		if(toolTipMenu != null) toolTipMenu.hide();
	    }
    }
    
    //-------------------------------------------------------------------------
    
    private function catchButtonClick(e:Event):void 
		{
	mx.controls.Alert.show(e.target.data.label + " was clicked to take you to " + e.target.data.link);
    }

		
	]]>
</mx:Script>

</mx:Application>


附件为源文件


1. 设置ToolTip风格(通过css来设置)
   ToolTip { 
    fontFamily: "Arial"; 
    fontSize: 13; 
    fontStyle: "italic"; 
    color: #FF6699; 
    backgroundColor: #33CC99; 
}
当然这个可以通过css来动态设置例如:
StyleManager.getStyleDeclaration("ToolTip").setStyle("fontStyle","italic"); 
2. 错误提示errotTip

errorTip
{
border-style:"errorTipRight";
}
其中border-style其对应的格式决定小三角的方位,可选值:[toolTip,errorTipRight,errorTipAbove,errorTipBelow]
<mx:TextInput x="85" y="118" text="021-阿萨德发送" errorString="输入有误!" />
如果想修改源码,改变小三角的大小或者位置的话就需要自定义border-skin
ToolTip
{
border-skin:ClassReference("com.MyToolTipBorder");
}
其中 com.MyToolTipBorder其中是一个继承了RectangularBorder的类,源码参见附件, ToolTip默认的skin-class路径在sdk
sdks\3.0.0\frameworks\projects\framework\src\mx\skins\halo\ToolTipBorder
引入包的路径:import mx.skins.halo.ToolTipBorder;
笔者发现 ToolTipBorder 的类对于4个方向支持不是很好,其中最右边的提示,也就是小三角在右边的没有,把源码修改了下(附件中有MyToolTipBorder)
改变默认的错误提示的背景色设置4个方向的提示









3. 使用ToolTipManager动态创建自定义的tooltip;
使用ToolTipManager管理器类能够让你设置基本的ToolTip功能。比如显示延迟,以及控制是否显示ToolTip信息内容。
你可以在程序中激活和停止使用ToolTips.
可以使用enabled这个属性来激活或者停止使用ToolTips,如果停止使用,在鼠标移到到组件上方的时候就不会用ToolTips的框显示出来,不管那个组件是否设置了toolTip这个属性值
设置ToolTipManager的常用属性介绍
ToolTipManager.enabled = true;//鼠标移入控件自动显示工具提示
ToolTipManager.showDelay = 0; //flex 等待tooltip出现的时间单位毫秒默认500
ToolTipManager. hideDelay=3000; //flex 隐藏tooltip所需的事件等待量 单位毫秒,默认500
ToolTipManager.showEffect= fadeIn; //显示tooltip的特效
ToolTipManager. hideEffect= fadeIn; //隐藏 tooltip的特效
<mx:Fade id="fadeIn" alphaFrom="0" alphaTo="1" duration="1000"/>
4. 使用ToolTipManager createToolTip,destroyToolTip来动态创建自定义tooltip
一般自定义tooltip都是在控件的
MouseEvent. ROLL_OVER(rollOver)
MouseEvent. MOUSE_OVER (mouseOver)
控制自定义tooltip的显示
MouseEvent. ROLL_OUT (rollOut)
MouseEvent. MOUSE_OUT (mouseOut)
控制自定义tooltip的隐藏
现已rollOver结合ToolTipManager实现自定义tooltip
Xml代码 复制代码 收藏代码
  1. <mx:Script> 
  2.     <![CDATA[
  3. import mx.managers.ToolTipManager;
  4. public var myTip:ToolTip;
  5. private function showTip(event:MouseEvent,type:String):void
  6. {
  7. Tip=ToolTipManager.createToolTip(str,event.stageX,event.stageY) as ToolTip;
  8. //根据需要动态设置tooltip的style属性
  9. myTip.move(event.stageX,event.stageY);
  10. }
  11. private function hideTip():void
  12.     {
  13.         ToolTipManager.destroyToolTip(myTip);
  14.     }
  15.     ]]> 
  16. </mx:Script> 
<mx:Script>
	<![CDATA[
import mx.managers.ToolTipManager;
public var myTip:ToolTip;
private function showTip(event:MouseEvent,type:String):void
{
Tip=ToolTipManager.createToolTip(str,event.stageX,event.stageY) as ToolTip;
//根据需要动态设置tooltip的style属性
myTip.move(event.stageX,event.stageY);
}
private function hideTip():void
	{
		ToolTipManager.destroyToolTip(myTip);
	}
	]]>
</mx:Script>

5. 实现mx.core.IToolTip接口自定义toolTip的外观(可选as实现该接口,mxml实现该接口)
现已mxml实现该接口为例(简单), MyPanelToolTip.mxml
Xml代码 复制代码 收藏代码
  1. <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"   
  2.     implements="mx.core.IToolTip"   
  3.     width="212"   
  4.     borderThickness="2"   
  5.    backgroundColor="0xCCCCCC"   
  6.    dropShadowEnabled="true"   
  7.    borderColor="black"   
  8.    roundedBottomCorners="true" 
  9.     height="90">    
  10.    <mx:Script> 
  11.    <![CDATA[ 
  12.    //注意 Panel 中的  implements="mx.core.IToolTip" 
  13.    [Bindable]  
  14.    private var _text:String=""; 
  15.    public function get text():String { 
  16.    return _text ;
  17.    } 
  18.    public function set text(value:String):void {
  19.         this._text=value; 
  20.    } 
  21.    ]]></mx:Script>   
  22.    <mx:Text text="{text}" percentWidth="100"/>   
  23.    </mx:Panel> 
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"  
    implements="mx.core.IToolTip"  
    width="212"  
    borderThickness="2"  
   backgroundColor="0xCCCCCC"  
   dropShadowEnabled="true"  
   borderColor="black"  
   roundedBottomCorners="true"
    height="90">   
   <mx:Script>
   <![CDATA[  
   //注意 Panel 中的  implements="mx.core.IToolTip"  
   [Bindable]   
   private var _text:String="";  
   public function get text():String {  
   return _text ;
   }  
   public function set text(value:String):void {
   		this._text=value;  
   }  
   ]]></mx:Script>  
   <mx:Text text="{text}" percentWidth="100"/>  
   </mx:Panel>

使用时:
private function createCustomTip(title:String, body:String, event:ToolTipEvent):void
{
var pan:MyPanelToolTip = new MyPanelToolTip();
           pan.title = title;
           pan.text=body;
           event.toolTip = pan;
}
<mx:Button id="btn3" label="button" x="275.5" toolTip="demo" toolTipCreate="createCustomTip('自定义tooltip','该tooltip实现mx.core.IToolTip接口,注意是控件必须设置toolTip属性的值',event)" y="187"/>
6. 自定义组件(继承UIComponent)结合mouseover事件形式  
效果图:




zhuan zi : http://demojava.iteye.com/blog/1181955
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值