简体   繁体   中英

How to control cursor (carat) position in TextInput in Flex 4.5

I need to handle diagraphs and then convert them on the fly to the proper unicode representation. For example when the user types in:

Sx

My app needs to replace it with:

Ŝ

Now, I've been able to do the replacement no problem. The issue though is that once I've done the replacement, the cursor goes to the beginning of the textbox rather than the end. As I'm trying to update the user's text on the fly, this obvious doesn't work.

How can I get it so that once I replace the text in the TextInput box, the cursor is on the right hand side rather than the left?

Found a solution.

All you have to do is instead of updating the whole text, wipe the current content and then use:

textInput.appendText()

Hopefully this will help someone else :)

The setSelection method is how you set the cursor

textInput.setSelection(textInput.text.length, textInput.text.length);

You can get the current beginning of the selection with TextInput.selectionAnchorPosition and the end of the selection with TextInput.selectionAnchorPosition

Take a look at this SO Question: How do you programmatically move the caret of a Flex TextArea to the end?

If you are using a textArea then this will work (from the selected answer):

textArea.selectionBeginIndex = textArea.length;
textArea.selectionEndIndex = textArea.length;

对于来这里寻找Spark textInput解决方案的人来说,这就是这样的:

textInput.selectRange(textInput.text.length, textInput.text.length);

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