<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Panel title="Weather Report" horizontalCenter="-56" verticalCenter="-27" id="myPanel" width="600" height="450">
<mx:TextInput id="cityname" text="大连" fontSize="12"/>
<mx:Button click="webService.getWeatherbyCityName(cityname.text)" icon="@Embed('image/r_t1.gif')" />
<!--<mx:TextArea id="myWeatherReport" height="450" editable="false" fontSize="12" backgroundColor="#ffd7ff" color="#0000ff" width="600" /> -->
<mx:Panel title="Rusult" width="100%" height="320">
<mx:VBox id="vbox" fontSize="12">
</mx:VBox>
</mx:Panel>
</mx:Panel>
<mx:WebService id="webService" wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
showBusyCursor="true" result="onLoad(event)" fault="faultHandler(event)" />
<mx:Style>
ToolTip {
fontFamily: "Arial";
fontSize: 14;
/*fontStyle: "italic";*/
color: #0000ff;
backgroundColor: #ffd7ff;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.utils.ArrayUtil;
import mx.controls.*;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.core.UIComponent;
import flash.utils.getDefinitionByName;
private function onLoad(event:ResultEvent):void{
vbox.removeAllChildren();
//Alert.show(event.result.toString(),"WebService Results");
var weatherInfo:ArrayCollection = (event.result) as ArrayCollection;
for(var i:int=0;i <weatherInfo.length;i++){
if(weatherInfo[i].toString().lastIndexOf(".gif")==1){
var imgcls:Class = getDefinitionByName("mx.controls.Image") as Class;
var imginstance:UIComponent = new imgcls();
Image(imginstance).source= "image/"+weatherInfo[i];
vbox.addChild(imginstance);
}else {
var cls:Class = getDefinitionByName("mx.controls.Label") as Class;
var instance:UIComponent = new cls();
Label(instance).text = weatherInfo[i];
Label(instance).width = 540;
ToolTip.maxWidth = 800;//设置Label.ToolTip最大宽度
if(Label(instance).text.length>500) {
Label(instance).addEventListener(ToolTipEvent.TOOL_TIP_CREATE,function (event:ToolTipEvent):void{createToolTip(event,Label(instance).text)});//调用自定义ToolTip
}
vbox.addChild(instance);
//Label(instance).truncateToFit = false;
}
//myWeatherReport.text+=weatherInfo[i]+"\n";
}
}
private function faultHandler(event:FaultEvent):void{
Alert.show(event.fault.toString(),"WebService Error");
}
import mx.events.ToolTipEvent;
//create user-defined ToolTip
private function createToolTip(e:ToolTipEvent,arg:String):void
{
var tip:MyToolTip = new MyToolTip();
tip.text=arg;
e.toolTip = tip;
}
//ToolTip position
private function positionToolTip(e:ToolTipEvent):void
{
e.toolTip.x = mouseX + 10;
e.toolTip.y = mouseY + 20;
}
]]>
</mx:Script>
</mx:Application>
====================自定义ToolTip组件"MyToolTip.mxml"=======================
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.core.IToolTip"
width="800" height="100%" backgroundColor="#ffd7ff" borderStyle="solid" cornerRadius="5">
<mx:Script>
<![CDATA[
[Bindable]
private var _text:String;
public function get text():String
{
return this._text;
}
public function set text(value:String):void
{
this._text = value;
}
]]>
</mx:Script>
<mx:Text text="{this._text}" color="#0000ff" fontSize="14" textAlign="left" width="798"/>
</mx:Canvas>