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>
          自定義格式化組件需要擴展mx.formatters.Formatter,并且重寫format方法
          如果發生格式化錯誤,會給error屬性返回一個字符串信息提示

          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>
          自定義驗證組件需要擴展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 長春語林科技 閱讀(300) 評論(0)  編輯  收藏 所屬分類: flex
          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 余庆县| 景德镇市| 邹城市| 大城县| 镇安县| 定南县| 积石山| 镇平县| 广河县| 马边| 资溪县| 宕昌县| 蓝田县| 和静县| 泽库县| 盈江县| 镇沅| 泸西县| 广汉市| 建瓯市| 天长市| 康保县| 简阳市| 瑞安市| 平昌县| 郴州市| 南靖县| 美姑县| 博罗县| 镇江市| 马边| 于都县| 甘泉县| 铁岭市| 万源市| 天镇县| 永春县| 宁津县| 宜君县| 延长县| 浙江省|