Android: Edittext- get current line

前端 未结 3 2084
说谎
说谎 2021-01-05 20:10

In an edittext is there a method for getting the current line of the cursor? If not I will write my own method, but just wanted to check. If I do write my own method would t

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 20:29

    Just to let people know:

    There is a better way to do this then Doug Paul has suggested by using the 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;
    }
    

提交回复
热议问题