Flex: 只接受数字的文本输入

24

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


另请参见:https://dev59.com/81jUa4cB1Zd3GeqPOBh4 - Even Mien
8个回答

31

看一下TextInput类的restrict属性。将其设置为“0-9”。


是的,如果我没记错的话,它只是“.0-9”。请注意,如果您这样做,他们将能够添加多个小数点。如果您要限制他们创建合法数字,则需要一些额外的 AS 来处理它。 - Gregor Kiddie

13
   <s:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />
   <mx:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />

3

2
<?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>

1
我使用类似以下的代码:

<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;
    }

该变更监听器函数从小数点后的精度数字开始删除所有内容,或删除任何第二个出现的“.”:


0

我不确定你想要做什么。如果你只是想把它们相加,使用以下方法

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

你的例子只是将这两个字符串连接起来。而这个例子则试图将文本转换为数字,然后将这两个值相加。


0

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

尝试使用 'SoftKeyboard"number" ; '


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接