Android EditText - Focus on it and select all text programmatically

后端 未结 3 2118
有刺的猬
有刺的猬 2021-02-19 09:26

I am new to Android and I am trying to figure out how do I focus on EditText and select all text in it.

Here is the scenario:

  1. User enters user name in E

相关标签:
3条回答
  • 2021-02-19 09:42

    You can also add android:selectAllOnFocus="true" to the EditText

    0 讨论(0)
  • 2021-02-19 09:53

    For selection of all the text of Edittext, you can use

    editText.setSelection(0,editText.getText().toString().length());

    and if you want to set cursor position you can use

    editText.setSelection(position);

    0 讨论(0)
  • 2021-02-19 09:57

    I figured it out. Here is what I did.

    I have a button user clicks when he enters user name (the positive button on DialogFragment). On button click event, I do this:

    editUserName.setSelectAllOnFocus(true);
    editUserName.selectAll();
    

    This will focus and select all text in the EditText.

    0 讨论(0)
提交回复
热议问题