How to set cursor position in EditText?

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

    This code will help you to show your cursor at the last position of editing text.

     editText.requestFocus();
     editText.setSelection(editText.length());
    
    0 讨论(0)
  • 2020-11-28 03:16

    I want to set cursor position in edittext which contains no data

    There is only one position in an empty EditText, it's setSelection(0).

    Or did you mean you want to get focus to your EditText when your activity opens? In that case its requestFocus()

    0 讨论(0)
  • 2020-11-28 03:17

    setSelection(int index) method in Edittext should allow you to do this.

    0 讨论(0)
  • 2020-11-28 03:19

    I'm so late to answer this problem, so I figure it out. Just use,

    android:gravity="center_horizontal"

    0 讨论(0)
  • 2020-11-28 03:20

    I have done this way to set cursor position to end of the text after updating the text of EditText programmatically here, etmsg is EditText

    etmsg.setText("Updated Text From another Activity");
    int position = etmsg.length();
    Editable etext = etmsg.getText();
    Selection.setSelection(etext, position);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题