posts - 325,  comments - 25,  trackbacks - 0
          格式化
          SwitchSymbolFormatter 類

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
           creationComplete="format()" backgroundColor="#FFFFFF">

            <mx:Script>
              <![CDATA[
              import mx.formatters.SwitchSymbolFormatter;               
             
              private function format():void {
               // Create Instance of the SwitchSymbolFormatter
                var switchSymbolFormatter:SwitchSymbolFormatter = new SwitchSymbolFormatter();
                // Apply formatter on the unformatted TextInput data using selectedItem from ComboBox
                formatted.text = switchSymbolFormatter.formatValue(formatString.selectedItem.toString(), unformatted.text);
              }
              ]]>
            </mx:Script>

            <mx:Panel title="SwitchSymbolFormatter Example"  width="400" height="200"
              paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

            <mx:Form>
                <mx:FormItem label="Unformatted Data:">
                  <mx:TextInput id="unformatted" text="123456789" editable="false"/>
                </mx:FormItem>
                <mx:FormItem label="Format String:">
                  <mx:ComboBox id="formatString" change="format()">
                    <mx:ArrayCollection>
                      <mx:String>#-########</mx:String>
                      <mx:String>###-######</mx:String>
                      <mx:String>##-###-####</mx:String>
                      <mx:String>#######-##</mx:String>
                  </mx:ArrayCollection>
                </mx:ComboBox>
                </mx:FormItem>
                <mx:FormItem label="Formatted Data:">
                  <mx:TextInput id="formatted" editable="false"/>
                </mx:FormItem>      
              </mx:Form>
              </mx:Panel>
          </mx:Application>
          如果要格式化的字符串中包含“#”,可用

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
           creationComplete="format()" backgroundColor="#FFFFFF">

            <mx:Script>
              <![CDATA[
              import mx.formatters.SwitchSymbolFormatter;               
             
              private function format():void {
               // Create Instance of the SwitchSymbolFormatter and pass in mask character
                var switchSymbolFormatter:SwitchSymbolFormatter = new SwitchSymbolFormatter("*");
                // Apply formatter on the unformatted TextInput data using selectedItem from ComboBox
                formatted.text = switchSymbolFormatter.formatValue(formatString.selectedItem..toString(), unformatted.text);
              }
              ]]>
            </mx:Script>

            <mx:Panel title="SwitchSymbolFormatter Example"  width="400" height="200"
              paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

            <mx:Form>
                <mx:FormItem label="Unformatted Data:">
                  <mx:TextInput id="unformatted" text="123456789" editable="false"/>
                </mx:FormItem>
                <mx:FormItem label="Format String:">
                  <mx:ComboBox id="formatString" change="format()">
                    <mx:ArrayCollection>
                      <mx:String># *-********</mx:String>
                      <mx:String># ***-******</mx:String>
                      <mx:String># **-***-****</mx:String>
                      <mx:String># *******-**</mx:String>
                  </mx:ArrayCollection>
                </mx:ComboBox>
                </mx:FormItem>
                <mx:FormItem label="Formatted Data:">
                  <mx:TextInput id="formatted" editable="false"/>
                </mx:FormItem>      
              </mx:Form>
              </mx:Panel>
          </mx:Application>
          自定義格式化組件需要擴(kuò)展mx.formatters.Formatter,并且重寫format方法
          如果發(fā)生格式化錯(cuò)誤,會(huì)給error屬性返回一個(gè)字符串信息提示

          package
          {
            import mx.formatters.Formatter;
            // Custom formatters must extend mx.formatters.Formatter
            public class ReverseFormatter extends Formatter {
                 
              public function ReverseFormatter() {
                super();
              }
              // Custom formatters must override format().
              override public function format(formatObj:Object):String         {
                if(formatObj.length == 0) {   
                  // return empty string and set error property if string has zero length.
                  error="Can not format an empty String";
                  return ""
                } else {
                 error=null;
                  var returnString:String = "";
                  // loop through value and build string in reverse
                  for(var i:Number=formatObj.length; i>=0; i--){
                    returnString = returnString + formatObj.charAt(i);
                  }
                  return returnString;
                }
              }
            }
          }
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
             backgroundColor="#FFFFFF" width="475" height="175" xmlns:comps="*">
           
            <mx:Script>
             <![CDATA[
              import mx.controls.Alert;
             
              private function format():void{
               reversetxt.text = reverseFormatter.format(validatetxt.text);
               if(reverseFormatter.error != null){
                Alert.show(reverseFormatter.error, "Formatter Error");
               }
              }
             ]]>
            </mx:Script>
           
            <comps:ReverseFormatter id="reverseFormatter" />
           
            <mx:Panel title="Reverse Formatter" width="100%" height="100%" layout="absolute">
              <mx:TextInput id="validatetxt" width="400" change="this.format()" y="30" x="3"/>
              <mx:TextInput id="reversetxt" width="400" x="3" y="60" editable="false"/>
              <mx:Label width="400" x="3" y="5" text="Type into the field below and see the results"/>
            </mx:Panel>
          </mx:Application>
          自定義驗(yàn)證組件需要擴(kuò)展mx.validators.Validator類,并重寫doValidation方法

          package
          {
              /*
              Authored by Rich Tretola
              */
              import mx.validators.Validator;
              import mx.validators.ValidationResult;

              public class PasswordValidator extends Validator {

                  private var results:Array;

                  public function PasswordValidator() {
                      super();
                  }
                  // Override the doValidation() method.
                  override protected function doValidation(value:Object):Array {

                      results = [];

                      // Call super's doValidation().
                      results = super.doValidation(value);       
                      // Return if super's doValidation contains errors (required would be an example).
                      if (results.length > 0){
                          return results;
                      }
                      // Check for min length
                      var dataString:String = String(value);
                      if (dataString.length < 6){
                       results.push(new ValidationResult(true, null, "Short", "Password must be at least 6 characters."));
                       return results;
                      }
                      // Check for max length (this can be set in the text component's maxChars property).
                      if (dataString.length > 10){
                       results.push(new ValidationResult(true, null, "Long", "Password must be no larger than 10 characters."));
                        return results;
                      }       

                      // Check for at least 1 upper case letter.
                      if (dataString.search("[A-Z]")<0) {
                          results.push(new ValidationResult(true, null, "Upper", "Passwords must contain at least one upper case letter."));
                          return results;
                      }
                      // Check for at least 1 lower case letter.
                      if (dataString.search("[a-z]")<0) {
                          results.push(new ValidationResult(true, null, "Lower", "Passwords must contain at lease one lower case letter."));
                          return results;
                      }
                      // Check for at least 1 number.
                      if (dataString.search("[0-9]")<0) {
                          results.push(new ValidationResult(true, null, "Number", "Passwords must contain at least one number."));
                          return results;
                      }
                      return results;
                  }
              }
          }

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
             backgroundColor="#FFFFFF" width="550" height="300" xmlns:comps="*">
           
            <comps:PasswordValidator required="true" source="{passwordtxt}" property="text"
              trigger="{validatebtn}" triggerEvent="click" listener="{passwordtxt}"/>
           
            <mx:Panel title="Password Validator" width="350" height="150">
             <mx:TextArea width="100%" height="45" editable="false" borderStyle="none"
              text="Password must be between 6-10 characters and contain at least 1 upper case, 1 lower case, and 1 number." />
              <mx:HBox paddingLeft="3">
                <mx:TextInput id="passwordtxt" width="75"/>
                <mx:Button id="validatebtn" label="Validate Password"/>   
              </mx:HBox>
            </mx:Panel>
          </mx:Application>

           

          posted on 2011-03-24 13:41 長(zhǎng)春語林科技 閱讀(299) 評(píng)論(0)  編輯  收藏 所屬分類: flex
          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

           

          長(zhǎng)春語林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 吉林市| 仙桃市| 镇江市| 闽侯县| 青冈县| 平安县| 汉川市| 游戏| 旬阳县| 彰武县| 苍溪县| 阿拉善右旗| 宁河县| 罗源县| 柘荣县| 田林县| 余庆县| 延长县| 军事| 通渭县| 哈巴河县| 仁布县| 桂林市| 南城县| 南涧| 兴文县| 塔河县| 内丘县| 潞西市| 瑞昌市| 铁力市| 新津县| 梅河口市| 白朗县| 新巴尔虎左旗| 清原| 崇文区| 吉安市| 江口县| 哈巴河县| 逊克县|