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

前端 未结 7 1057
無奈伤痛
無奈伤痛 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条回答
  •  臣服心动
    2021-02-01 03:21

    I had the same problem...I solved it by extending EditText.

    I created an onClickListener and in this method I've got the following code:

    public OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditTextInput.this.setFocusable(true);
            EditTextInput.this.setFocusableInTouchMode(true);
            EditTextInput.this.requestFocus();
        }
    };       
    

    I then placed the following code in the onKeyPreIme callback. You'll have to extend EditText to get access to it.

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK & event.getAction() == KeyEvent.ACTION_UP) {
            EditTextInput.clearFocus();
            EditTextInput.this.setFocusable(false);
            EditTextInput.this.setFocusableInTouchMode(false);            
            return false;
        }
        return super.dispatchKeyEvent(event);
    }
    

    I find I have to toggle the focusable flag in order to bring the keyboard up on the very first click. Hope this helps...works for me.

提交回复
热议问题