Get Cursor Position in Android in Edit Text?

后端 未结 1 709
梦谈多话
梦谈多话 2020-11-28 05:42

I am using a custom EditText View. I have overridden the OnKeyUp event and am able to capture the Enter Key press. Now my requirement is, when the user has entered a text \"

相关标签:
1条回答
  • 2020-11-28 05:50

    You can get the Cursor position using the getSelectionStart() and getSelectionEnd() methods. If no text is highlighted, both getSelectionStart() and getSelectionEnd() return the position of the cursor. So something like this should do the trick:

    myEditText.getSelectionStart();
    

    or

    myEditText.getSelectionEnd();
    

    Then if you want to select all the text after the cursor you could use this:

    int cursorPosition = myEditText.getSelectionStart();
    CharSequence enteredText = myEditText.getText().toString();
    CharSequence cursorToEnd = enteredText.subSequence(cursorPosition, enteredText.length());
    
    0 讨论(0)
提交回复
热议问题