简体   繁体   English

如何在文本末尾设置插入位置?

[英]How to set the caret position at the end of the text?

I want to set the caret position to the end of a Text after appending text to a StyledText. 我想在将文本附加到StyledText后将插入符号位置设置为Text的结尾。

How do I achieve this? 我该如何实现这一目标? Set caret at the end of the text 在文本的末尾设置插入符号

This is in continuation to my earlier question 这是我早先的问题的延续

I have never used StyledText but looking at the API shows a method called setCaretOffset( int offset ) which takes a number that is the offset from the beginning of the text. 我从未使用过StyledText,但查看API会显示一个名为setCaretOffset( int offset )的方法,该方法采用的数字是文本开头的偏移量。 So you could call getText() , find its length, and then set the caret to be that number as the offset. 因此,您可以调用getText() ,查找其长度,然后将插入符号设置为该数字作为偏移量。

*I like the answer that Travis posted below. *我喜欢Travis在下面发布的答案。 I didn't notice the method getCharacterCount() on StyledSheet. 我没有注意到StyledSheet上的方法getCharacterCount()。 That is probably a little cheaper then getting the text then the length. 这可能比获得文本然后长度便宜一点。

I found a solution to my question. 我找到了解决问题的方法。

I tried this, but this was not preferable to me. 我尝试过这个,但这对我来说并不可取。

styledText.selectAll();//moves the scroll bar down but selects all text.

This worked for me! 这对我有用!

styledText.setSelection(styledText.getCharacterCount());

My gratitude to all for the above answers. 感谢所有人对上述答案的感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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