Unselect Flex TextInput with enter key
In Flex, how can one set the TextInput to "unselected" (which mean not 开发者_如何学Pythononly focus is out but also, that if you text on your keyboard, the TextInput won't be changed) on the event of pressing the Enter key? I know how to change the component that has the focus using the FocusManager, but I don't want to change the component that has focus, I just want this one component not to be selected.
I'm more of an AS3 dev, so I'm not sure if this works 100% in Flex, but you could use preventDefault() on the key handler and set the stage's focus to null.
textinput.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
protected function keyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.ENTER) {
event.preventDefault();
stage.focus = null;
}
}
textinput.setSelection(0,0)
You can use the setSelection() function which is available on MX TextInput and TextArea controls which has the function signature:
public function setSelection(beginIndex:int, endIndex:int):void
and takes in two parameters that denote the start and end zero-based indexes of the characters to select. The start index character will be included in the selection, and the end index is the index after the last character in your selection.
Using something like textinput.setSelection(0,0)
will essentially select nothing in the text input field. This will work in Flex 3.
If you are using Flex 4, however, then using the selectRange(anchorIndex:int, activeIndex:int)
function is more appropriate and functions the same. The difference between setSelection() and selectRange() is that the latter is done immediately. The selectRange() function will deselect the text range if either position is negative.
精彩评论