Android adjustpan not working after the first time

前端 未结 8 885
生来不讨喜
生来不讨喜 2020-12-01 05:55

My problem: starting from the second time the software keyboard is shown on the screen, it entirely hides my EditText.

Attribute android:windowSoftInputMode=\"adjus

相关标签:
8条回答
  • 2020-12-01 06:32

    Here is the code I was originally using that ran into this problem:

        <EditText
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:gravity="center" />
    

    When I removed the center gravity line from the EditText, the panning worked:

    android:gravity="center"
    

    I worked around it by wrapping the EditText in a FrameLayout and centering the entire EditText in the FrameLayout. The one issue it causes is that the EditText will be at least the width of the hinttext, so the first few characters/words typed won't be centered. This was fine for me but your mileage may vary :)

    <FrameLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content">
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </FrameLayout>
    

    EDIT: this workaround breaks if the user is clicking on the very last word in the edittext. I'm now using the onKeyPreIme workaround

    0 讨论(0)
  • 2020-12-01 06:36

    For me overriding onKeyPreIme doesn't work. So I override dispatchKeyEventPreIme instead.

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
         clearFocus();
        }
        return super.dispatchKeyEventPreIme(event);
    }
    
    0 讨论(0)
  • 2020-12-01 06:41

    The problem you're experiencing is now a known platform bug as described here: https://code.google.com/p/android/issues/detail?id=182191

    The fix is not backported and so will not take effect until Android N. Until Android N is your minimum SDK, the following solution is a workaround.

    You can create a custom class extending EditText and override the method onKeyPreIme(int keyCode, KeyEvent event) like this:

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event)
    {
       if(keyCode == KeyEvent.KEYCODE_BACK)
         {
             clearFocus();
         }
    return super.onKeyPreIme(keyCode, event);
    }
    

    You should also ensure a higher level layout has the ability to be focused so the EditText's focus may be cleared successfully. The following attributes may work for you:

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    

    Now when the back key is pressed, the EditText loses the focus. Then when you tap the button again, adjustpan will work as intended. (Posting as an answer since the edit was refused.)

    0 讨论(0)
  • 2020-12-01 06:43

    This may seem a bit silly but I ran into this problem when I set the property gravity of my EditText to either 'center_horizontal' or 'center'. The same problem occurs when using textAlignment 'center'. Remove it and you won't run into the problem of the keyboard hiding the EditText the second time (and subsequent ones) when the keyboard comes out.

    0 讨论(0)
  • 2020-12-01 06:44

    subclassed EditText and overridden the method onKeyPreIme(int keyCode, KeyEvent event) like this:

       @Override
       public boolean onKeyPreIme(int keyCode, KeyEvent event)
       {
          if(keyCode == KeyEvent.KEYCODE_BACK)
            {
                clearFocus();
            }
       return super.onKeyPreIme(keyCode, event);
       }
    

    Now when the back key is pressed, the EditText lost the focus. Then tapping it again adjustpan will work.

    0 讨论(0)
  • 2020-12-01 06:48

    Usually the adjust pan doesn't work after the first time when we press the back button try to do that following way:- (can be implemented for the EditText, AutoCompleteTextView or any other view in which misbehaves)

     @Override
        public void onBackPressed() {
            editText.clearFocus();
            editText.setCursorVisible(false);
        }
    

    Hope It helps someone

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