9
如何在Android中的EditText
中獲取光標位置x,y? (其中x是行#並且y是列#)如何在EditText中獲取光標位置(x,y)android
如何在Android中的EditText
中獲取光標位置x,y? (其中x是行#並且y是列#)如何在EditText中獲取光標位置(x,y)android
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位置。
你可以找到當前選擇的行數和這樣的色譜柱:
// 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);
你說的(X,Y),參考一光標是什麼意思? (行號,列#)? – Devunwired 2011-02-18 17:28:18
是的,我想要得到遊標(Line#,Column#)。你可以幫我嗎? – hieubk 2011-02-19 01:57:34