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

前端 未结 7 1040
無奈伤痛
無奈伤痛 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:06

    In addition to android:windowSoftInputMode="adjustPan", try below:

    1. Set android:descendantFocusability="afterDescendants" to your ListView.
    2. Set android:focusable="true" and android:focusableInTouchMode="true" to your EditText.
    0 讨论(0)
  • 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);
            }
        }
    });
    
    0 讨论(0)
  • 2021-02-01 03:13

    You could try something like :

    editText.requestFocus();
    

    You could also set this :

    android:windowSoftInputMode="stateHidden"
    

    in your manifest file

    0 讨论(0)
  • 2021-02-01 03:16

    You need to change in your AndroidManifest.xml

    Add android:windowSoftInputMode="adjustPan" in the activity holding the listview. This will solve your problem.

    <activity android:name=".MyEditTextInListView"
              android:label="@string/app_name"
              android:windowSoftInputMode="adjustPan">
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 03:21

    I had this problem with android.support.design.widget.TextInputEditText and it was solved from changing the flag order in manifest from:

    android:windowSoftInputMode="stateHidden|adjustPan"
    

    to

    android:windowSoftInputMode="adjustPan|stateHidden"
    
    0 讨论(0)
提交回复
热议问题