Flex Gumbo中如何利用export()以及E4X遍历FxTextArea内的段落的例子

By Minidxer | June 1, 2009

接下来的例子演示了Flex Gumbo中如何利用export()以及E4X,遍历FxTextArea内的段落。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Application name="FxTextArea_export_test"
  3.         xmlns="http://ns.adobe.com/mxml/2009"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white">
  7.  
  8.     <Script>
  9.         private function getParagraphs():void {
  10.             var textFlowNS:Namespace = new Namespace("http://ns.adobe.com/textLayout/2008");
  11.  
  12.             var xml:XML = textArea.export();
  13.             var xmlList:XMLList = xml.textFlowNS::p;
  14.             var para:XML;
  15.             for each (para in xmlList) {
  16.                 para.@color = getRandomColor();
  17.             }
  18.             textArea.content = xml;
  19.         }
  20.  
  21.         private function getRandomColor():String {
  22.             return "0x" + Math.round(Math.random() * 0xFFFFFF).toString(16);
  23.         }
  24.     </Script>
  25.  
  26.     <ApplicationControlBar dock="true">
  27.         <FxButton label="Randomize paragraph colors"
  28.                 click="getParagraphs();" />
  29.     </ApplicationControlBar>
  30.  
  31.     <FxTextArea id="textArea" width="240" height="160">
  32.         <content>
  33.             <p>The quick brown fox jumped over the lazy dog.</p>
  34.             <p>The quick brown fox jumped over the lazy dog.</p>
  35.             <p>The quick brown fox jumped over the lazy dog.</p>
  36.             <p>The quick brown fox jumped over the lazy dog.</p>
  37.             <p>The quick brown fox jumped over the lazy dog.</p>
  38.             <p>The quick brown fox jumped over the lazy dog.</p>
  39.             <p>The quick brown fox jumped over the lazy dog.</p>
  40.             <p>The quick brown fox jumped over the lazy dog.</p>
  41.             <p>The quick brown fox jumped over the lazy dog.</p>
  42.             <p>The quick brown fox jumped over the lazy dog.</p>
  43.             <p>The quick brown fox jumped over the lazy dog.</p>
  44.             <p>The quick brown fox jumped over the lazy dog.</p>
  45.             <p>The quick brown fox jumped over the lazy dog.</p>
  46.             <p>The quick brown fox jumped over the lazy dog.</p>
  47.             <p>The quick brown fox jumped over the lazy dog.</p>
  48.         </content>
  49.     </FxTextArea>
  50.  
  51. </Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: FxTextArea, Gumbo | No Comments » | Tags: , , , ,

Search Posts