處理 FTE 中的事件

Flash Player 10 以及更新的版本,Adobe AIR 1.5 以及更新的版本

您可以將事件偵聽程式新增至 TextLine 實體,就如同處理其他顯示物件一樣。例如,您可以偵測使用者何時將滑鼠指向文字行上方,或使用者何時按一下該行。下列範例將會偵測這兩個事件。當您將滑鼠指向該行上方時,游標會變成按鈕游標,而當您按一下該行時,游標會改變顏色。
package 
{ 
    import flash.text.engine.*; 
    import flash.ui.Mouse; 
    import flash.display.Sprite 
    import flash.events.MouseEvent; 
    import flash.events.EventDispatcher; 
     
    public class EventHandlerExample extends Sprite 
    { 
        var textBlock:TextBlock = new TextBlock(); 
 
        public function EventHandlerExample():void 
        { 
            var str:String = "I'll change color if you click me."; 
            var fontDescription:FontDescription = new FontDescription("Arial"); 
            var format:ElementFormat = new ElementFormat(fontDescription, 18); 
            var textElement = new TextElement(str, format); 
            textBlock.content = textElement; 
            createLine(textBlock); 
        } 
     
        private function createLine(textBlock:TextBlock):void 
        { 
            var textLine:TextLine = textBlock.createTextLine(null, 500); 
            textLine.x = 30; 
            textLine.y = 30; 
            addChild(textLine); 
            textLine.addEventListener("mouseOut", mouseOutHandler); 
            textLine.addEventListener("mouseOver", mouseOverHandler); 
            textLine.addEventListener("click", clickHandler); 
        } 
 
        private function mouseOverHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "button"; 
        } 
 
        private function mouseOutHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "arrow"; 
        } 
 
        function clickHandler(event:MouseEvent):void { 
            if(textBlock.firstLine) 
                removeChild(textBlock.firstLine); 
            var newFormat:ElementFormat = textBlock.content.elementFormat.clone(); 
            switch(newFormat.color) 
            { 
                case 0x000000: 
                    newFormat.color = 0xFF0000; 
                    break; 
                case 0xFF0000: 
                    newFormat.color = 0x00FF00; 
                    break; 
                case 0x00FF00: 
                    newFormat.color = 0x0000FF; 
                    break; 
                case 0x0000FF: 
                    newFormat.color = 0x000000; 
                    break; 
            } 
            textBlock.content.elementFormat = newFormat; 
            createLine(textBlock); 
        } 
    } 
}

鏡像事件

您也可以在將事件發送器上,建立文字區塊或一部分文字區塊的事件鏡像。首先,建立 EventDispatcher 實體,然後將它指定給 TextElement 實體的 eventMirror 屬性。如果文字區塊包含單一文字元素,則此文字引擎會鏡像整個文字區塊的事件。如果文字區塊包含多個文字元素,則此文字引擎只會鏡像已設定 eventMirror 屬性的 TextElement 實體事件。下列範例中的文字包含三個元素:單字 "Click"、單字 "here" 和字串 "to see me in italic"。此範例會將事件發送器指定給第二個文字元素「here」,並新增事件偵聽程式 (clickHandler() 方法)。clickHandler() 方法會將文字變更為斜體。它也會以「Click here to see me in normal font!」取代第三個文字元素的內容。

package 
{ 
    import flash.text.engine.*; 
    import flash.ui.Mouse; 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.events.EventDispatcher; 
     
    public class EventMirrorExample extends Sprite 
    {             
        var fontDescription:FontDescription = new FontDescription("Helvetica", "bold"); 
        var format:ElementFormat = new ElementFormat(fontDescription, 18); 
        var textElement1 = new TextElement("Click ", format); 
        var textElement2 = new TextElement("here ", format); 
        var textElement3 = new TextElement("to see me in italic! ", format); 
        var textBlock:TextBlock = new TextBlock(); 
 
        public function EventMirrorExample() 
        { 
            var myEvent:EventDispatcher = new EventDispatcher(); 
             
            myEvent.addEventListener("click", clickHandler); 
            myEvent.addEventListener("mouseOut", mouseOutHandler); 
            myEvent.addEventListener("mouseOver", mouseOverHandler); 
             
            textElement2.eventMirror=myEvent; 
             
            var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>; 
            groupVector.push(textElement1, textElement2, textElement3); 
            var groupElement:GroupElement = new GroupElement(groupVector); 
             
            textBlock.content = groupElement; 
            createLines(textBlock); 
        } 
             
        private function clickHandler(event:MouseEvent):void 
        { 
            var newFont:FontDescription = new FontDescription(); 
            newFont.fontWeight = "bold"; 
         
            var newFormat:ElementFormat = new ElementFormat(); 
            newFormat.fontSize = 18; 
            if(textElement3.text == "to see me in italic! ") { 
                newFont.fontPosture = FontPosture.ITALIC; 
                textElement3.replaceText(0,21, "to see me in normal font! "); 
            } 
            else { 
                newFont.fontPosture = FontPosture.NORMAL; 
                textElement3.replaceText(0, 26, "to see me in italic! "); 
            } 
            newFormat.fontDescription = newFont; 
            textElement1.elementFormat = newFormat; 
            textElement2.elementFormat = newFormat; 
            textElement3.elementFormat = newFormat; 
            createLines(textBlock); 
        } 
             
        private function mouseOverHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "button"; 
        } 
             
        private function mouseOutHandler(event:MouseEvent):void 
        { 
                Mouse.cursor = "arrow"; 
        } 
             
        private function createLines(textBlock:TextBlock):void 
        { 
            if(textBlock.firstLine) 
                removeChild (textBlock.firstLine); 
            var textLine:TextLine = textBlock.createTextLine (null, 300); 
            textLine.x = 15; 
            textLine.y = 20; 
            addChild (textLine); 
        } 
    } 
}

mouseOverHandler()mouseOutHandler() 函數會在游標停留在「here」這個字上方時將游標設為按鈕游標,而在離開時設回箭號。