setSelection in EditText's onFocusChange

前端 未结 2 1867
半阙折子戏
半阙折子戏 2021-01-20 15:55

Normally, when the view is clicked, the text cursor is set near the place you clicked on.
I try to set it always to the end (past the last character), but it does nothin

相关标签:
2条回答
  • 2021-01-20 16:08

    No need delay. we need to wait setSelection or else automatically called done and execute my setSelection.

    new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                final EditText et = (EditText) v;
                et.post(new Runnable() {
                    @Override
                    public void run() {
                        et.setSelection(et.getText().length());
                    }
                });
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-20 16:32

    You should call setSelection() inside the afterTextChanged() of the addTextChangedListener().

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