简体   繁体   中英

Get textinput number of character in Flex

I have textinput in flex we application.

<s:TextInput id="txtGuestCount" maxChars="2" editable="false"/>

Where i limit maxChars to 2. It works when i remove editable="false" from textinput and type using keyboard.

Problem:
But same thing is not works when i do using button click like:

<s:Button width="50" height="50"  chromeColor="#ffffff" label="1" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="2" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="3" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="4" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="5" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="6" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="7" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="8" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="9" buttonMode="true" click="onNumberClick(event)"/>
<s:Button width="50" height="50"  chromeColor="#ffffff" label="0" buttonMode="true" click="onNumberClick(event)"/>

On click of button insertlabel value in textinput like:

protected function onNumberClick(event:MouseEvent):void
{
   txtGuestCount.text += event.currentTarget.label;
}

The above thing is not working. It will insert more than 2 character in textInput .

Any one help me to figure-out this issue?
How can i restrict max 2 character in textinput?

According to the manual , maxChars is not checked when text is altered in code. So, you have to manually control the new value of text to not exceed 2 chars. An example:

protected function onNumberClick(event:MouseEvent):void
{
    var s:String=txtGuestCount.text;
    if (s.length==2) s=s.substr(1); // drop first char
    s+=event.currentTarget.label;
    // if (s.length>2) s=s.substr(0,2); // if necessary, truncate
    txtGuestCount.text = s;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM