【问题标题】:Flex: Text Input that accepts number onlyFlex:仅接受数字的文本输入
【发布时间】:2010-12-17 09:06:15
【问题描述】:

需要一个只接受数字的代码。输入时,code必须检查是否是数字,如果不是,则必须删除输入的键或根本不输入

【问题讨论】:

标签: apache-flex actionscript textinput


【解决方案1】:

查看TextInput 类的restrict 属性。设置为“0-9”

【讨论】:

  • 是的,如果我没记错的话,它只是“.0-9”。请注意,他们将能够添加多个 .如果你这样做。如果您限制他们制作合法号码,则需要一些额外的 AS 来处理它。
【解决方案2】:
   <s:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />
   <mx:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />

【讨论】:

    【解决方案3】:

    有一个名为 NumericStepper 的控件。

    见:http://livedocs.adobe.com/flex/3/html/help.html?content=controls_11.html

    如果您不希望出现向上和向下箭头,可以将其外观类设置为 null。

    干杯, 狡猾

    【讨论】:

      【解决方案4】:
      <?xml version="1.0"?>
      <!-- Simple example to demonstrate the TextInput control. -->
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html">
      
          <mx:Panel title="Dodawanie dwóch liczb :)" height="279" width="238" 
              paddingTop="10" paddingLeft="10">
      
              <mx:TextInput id="src"
                restrict="0-9"
                      maxChars="20" />
              <mx:TextInput id="dest"
                restrict="0-9"
                      maxChars="20"/>
      
              <mx:Button label="dodaj" click= "dodaj();" id="but"/>
              <mx:Label text="Suma" width="59"/>
              <mx:Label text="0" width="160" id="wynik"/>
      
          </mx:Panel>
          <mx:Script>
           <![CDATA[
            import mx.formatters.NumberBase;
            public function dodaj():Number
            {
             var liczba:Number = Number(src.text) + Number(dest.text);
             wynik.text = liczba.toString();
             return 0;
            }
      
           ]]>
          </mx:Script>
      </mx:Application>
      

      【讨论】:

        【解决方案5】:

        【讨论】:

          【解决方案6】:

          我使用类似的东西

          <s:TextInput id="textInput"
              restrict="0-9.\\-"
              change="onChangeNumberTextInput(event, 6)"/>
          
          private function onChangeNumberTextInput(event:TextOperationEvent, precision:uint = 2):void
              {
                  var strNumber:String = "";
                  if (event.currentTarget is mx.controls.TextInput)
                      strNumber = (event.currentTarget as mx.controls.TextInput).text;
                  else if (event.currentTarget is spark.components.TextInput)
                      strNumber = (event.currentTarget as spark.components.TextInput).text;
                  else
                      return;
          
                  var ind:int = strNumber.indexOf(".");
                  if (ind > -1)
                  {
                      var decimal:String = strNumber.substring(ind + 1);
                      if (decimal.indexOf(".") > -1)
                          strNumber = strNumber.substring(0, ind + 1 + decimal.indexOf("."));
                      if (decimal.length > precision)
                          strNumber = strNumber.substring(0, ind + 1 + precision);
                  }
          
                  if (event.currentTarget is mx.controls.TextInput)
                      (event.currentTarget as mx.controls.TextInput).text = strNumber;
                  else if (event.currentTarget is spark.components.TextInput)
                      (event.currentTarget as spark.components.TextInput).text = strNumber;
              }
          

          更改侦听器函数会删除小数点后的精度字符数或任何第二次出现的“.”之外的所有内容:

          【讨论】:

            【解决方案7】:

            您需要更改属性,以便应用程序仅向应用程序请求数字键盘。

            试试 'SoftKeyboard"number" ; '

            【讨论】:

              【解决方案8】:

              我不确定你到底想做什么。如果您只想将这两者相加,请使用以下

              {parseInt(txt1.text) + parseInt(txt2.text)}
              

              您的示例只是连接这两个字符串。这个示例尝试将文本转换为数字,然后将这两个值相加。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-12-15
                • 1970-01-01
                • 2014-03-26
                • 1970-01-01
                • 1970-01-01
                • 2017-05-18
                • 2022-08-12
                • 1970-01-01
                相关资源
                最近更新 更多