简体   繁体   中英

Getting the left part text of an EditText with Spans

My Android app has an EditText in its layout.

The EditText can contain text with optional ImageSpans.

At a certain point the user can perform some editing, that is, text can be edited and/or spans can be removed.

I get the caret position with

 int caretPosition= editText.getSelectionStart();

I need to know at this point, the text on the left, without the spans.

I mean, if no spans are present on the left the returned text is just what is expected, while if some spans are present, the text has no spaces or other characters in place of spans.

Example

aaaaaaaaaaa<span>bbbbbb<span>cccc_dddddd<span>eeeeee

the character _ is here for the cursor position (but it has not any space).

My goal is to get this string:

aaaaaaaaaaabbbbbbcccc

It is difficult because it seems that spans are like a single char, but I am not sure. And I do not know how to split an editable to iterate on the left part.

How to achieve that?

I think this is the solution:

int caretPosition=editText.getSelectionStart();
Spannable spannable=editText.getText();
String leftPartWithoutSpans=spannable.subString(0,caretPosition).toString();

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