[ Android ] How To Insert Text in Cursor Position

The most correct way to do that is something like that:

String text="Some Text";
editText.getText().insert(editText.getCursorPosition(), text);

Why? Because it’s will return to us original Editable source:

public class EditText extends TextView {
//....
    @Override
    public Editable getText() {
        return (Editable) super.getText();
    }
//....
}
public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {
//...
    @ViewDebug.CapturedViewProperty
    public CharSequence getText() {
        return mText;
    }
//...
}

All other ways, which include replacement of old text with setText — is not recommended.

 

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *