Android:Edittext-获取当前行

Hen*_*son 4 android android-edittext

在edittext中有一种获取光标当前行的方法吗?如果不是,我会写自己的方法,但只是想检查.如果我编写自己的方法,最好的方法是遍历edittext中的每个字符,直到selectionstart并使用For循环计算\n的数量,或者有更好的方法吗?谢谢!

Hen*_*son 14

只是为了让人们知道:

有一个更好的方法来做到这一点然后道格保罗建议使用getLineForOffset(selection):

public int getCurrentCursorLine(EditText editText)
{    
    int selectionStart = Selection.getSelectionStart(editText.getText());
    Layout layout = editText.getLayout();

    if (!(selectionStart == -1)) {
        return layout.getLineForOffset(selectionStart);
    }

    return -1;
}
Run Code Online (Sandbox Code Playgroud)