How to set cursor position in EditText?

前端 未结 23 1584
广开言路
广开言路 2020-11-28 02:51

There are two EditText,while loading the page a text is set in the first EditText, So now cursor will be in the starting place of EditText, I want

23条回答
  •  心在旅途
    2020-11-28 03:23

    If you want to place the cursor in a certain position on an EditText, you can use:

    yourEditText.setSelection(position);
    

    Additionally, there is the possibility to set the initial and final position, so that you programmatically select some text, this way:

    yourEditText.setSelection(startPosition, endPosition);
    

    Please note that setting the selection might be tricky since you can place the cursor before or after a character, the image below explains how to index works in this case:

    So, if you want the cursor at the end of the text, just set it to yourEditText.length().

提交回复
热议问题