简体   繁体   中英

Android get focused word from edittext

Lets say I have an Android edittext with sentence: "I like Stackoverflow very much". Then I use my keyboard to be focused on the word Stackoverflow and now I click the button in my app that adds / removes for example bold effect for this specific word.

Is it possible to do that?

Thank you in advance.

if you are talking about position of cursor then use

int cursorPosition = et.getSelectionStart();

after taking position of cursor you should take whole text and apply desired effect. for example split text by white characters (spaces, enters), calculate on which word cursor is pointing and add/remove bold spannable

note that et.getText() returns Editable , not just usual String . So you can do some work on this class, eg check if word with cursor inside is already bold or other spannable applied

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