How to set cursor position in EditText?

前端 未结 23 1506
广开言路
广开言路 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:21

    How to Set EditText Cursor position in Android

    Below Code is Set cursor to Starting in EditText:

     EditText editText = (EditText)findViewById(R.id.edittext_id);
     editText.setSelection(0);
    

    Below Code is Set cursor to end of the EditText:

    EditText editText = (EditText)findViewById(R.id.edittext_id);
    editText.setSelection(editText.getText().length());
    

    Below Code is Set cursor after some 2th Character position :

     EditText editText = (EditText)findViewById(R.id.edittext_id);
     editText.setSelection(2);
    

提交回复
热议问题