2011-02-18 4 views
9

안드로이드에서 EditText의 커서 위치 x, y를 얻으려면 어떻게해야합니까? (여기서 x는 행 번호이고 y는 열 번호입니다.)EditText에서 커서 위치 (x, y)를 얻는 방법 android

+1

당신은 커서 참조 (x, y)는 무엇을 의미합니까? (라인 #, 컬럼 #)? – Devunwired

+0

예! 커서를 가져오고 싶습니다 (Line #, Column #). 저를 도울 수 있습니까? – hieubk

답변

29
int pos = editText.getSelectionStart(); 
Layout layout = editText.getLayout(); 
int line = layout.getLineForOffset(pos); 
int baseline = layout.getLineBaseline(line); 
int ascent = layout.getLineAscent(line); 
float x = layout.getPrimaryHorizontal(pos); 
float y = baseline + ascent; 

여기에 커서의 x, y 위치가 표시됩니다.

+0

이 질문에 대한 답변을 얻으면서 큰 도움이되었습니다. http://stackoverflow.com/questions/25036816/custom-rotated-edittext-view-with-working-selection-cursor-location-etc – Suragch

0

당신은 현재 선택의 라인 번호를 찾을 수와 같은 열이 :

// Get the current selection's line number 
int line = edittext.getLayout().getLineForOffset(edittext.getSelectionStart()); 

// Find the index for the start of the selected line. Subtract that from the current selection's index 
int column = edittext.getSelectionStart() - edittext.getLayout().getLineStart(line);