2009-11-17 6 views
23

숫자 만 허용하는 코드가 필요합니다. 입력 할 때 코드는 숫자인지 확인해야합니다. 숫자가 아닌 경우 입력 된 키를 제거하거나 전혀 입력하지 않아야합니다.플렉스 : 숫자 만 허용하는 텍스트 입력

+0

을 참조 : HTTP : //stackoverflow.com/questions/6300528/flex-restrict-textinput-to-accept-only-decimal-numberbers –

답변

30

TextInput 클래스의 restrict 속성을 확인하십시오. mx.validators.NumberValidator에서 "0-9"

+0

소수점은 어떻습니까? 저도 포함시킬 수 있습니까? – Treby

+0

네, 정확히 다시 말하면 ".0-9"입니다. 사용자는 둘 이상을 추가 할 수 있습니다. 네가 이렇게한다면. 합법적 인 번호를 만들도록 제한하는 경우 추가 AS가 필요합니다. –

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

내가 정확하게 뭘 원하는지 모르겠어요. 이 둘을 합치려면 다음을 사용하십시오.

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

예를 들어 두 문자열을 연결하면됩니다. 이 한 예는 텍스트를 숫자로 변환 한 다음이 두 값을 합산하려고 시도합니다.

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> 
0

당신은 응용 프로그램에만 응용 프로그램에서 숫자 키보드를 요청할 수 있도록 속성을 변경해야합니다.

시도 'SoftKeyboard "number"; '

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

변화 리스너 함수는 소수점에서 정밀 문자의 수, 또는의 두 번째 발생을 넘어 모든 것을 제거처럼 뭔가를해야만 사용 "."

관련 문제