开发者

How to get the position of selected character or String in EditText

开发者 https://www.devze.com 2023-03-14 09:23 出处:网络
I want to know how can I get the x,y position of the selected character or开发者_Go百科 String in

I want to know how can I get the x,y position of the selected character or开发者_Go百科 String in EditText. Is that possible?


Use this code to get the first index of a certain character:

String s = editText.getText().toString();
int position = s.indexOf("C");              // where C is your character to be searched


Here is how to get the x and y coordinates of a specific character in a TextView, should work for EditText as well. offset is the index of the desired character in the view's text.

Layout layout = editView.getLayout();
if (layout == null) { // Layout may be null right after change to the view
    // Do nothing
}

int lineOfText = layout.getLineForOffset(offset);
int xCoordinate = (int) layout.getPrimaryHorizontal(offset);
int yCoordinate = layout.getLineTop(lineOfText);
0

精彩评论

暂无评论...
验证码 换一张
取 消