由於項目需求,需要獲取EditText光標當前所在行行號,可是翻遍Android文檔、問遍度娘都沒發現,於是在博客園中提問,碰見了好心人告訴了我答案,謹以以下代碼獻給有需要的人
1 private int getCurrentCursorLine(EditText editText) { 2 int selectionStart = Selection.getSelectionStart(editText.getText()); 3 Layout layout = editText.getLayout(); 4 5 if (selectionStart != -1) { 6 return layout.getLineForOffset(selectionStart) + 1; 7 } 8 return -1; 9 }
作者:登天路