EditText loses focus when keyboard appears; requires touching twice to edit

前端 未结 7 1044
無奈伤痛
無奈伤痛 2021-02-01 02:49

I have been designing an application which holds an expandable list. At the end of every list, an empty EditText is ready to receive comments. I have the following

7条回答
  •  -上瘾入骨i
    2021-02-01 03:09

    A solution is to post a delayed runnable that checks to see if the EditText view is still focused, and if it is not, focus it.

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(final View v, boolean hasFocus) {
            if (hasFocus) {
                // Request focus in a short time because the
                // keyboard may steal it away.
                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                    }
                }, 200);
            }
        }
    });
    

提交回复
热议问题