Soft keyboard hides half of EditText

2019-01-17 15:52发布

问题:

I have a listview and the last list item contains EditText:

<RelativeLayout
    android:id="@+id/contentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/messageEditText"
        android:layout_below="@+id/messageEditText"
        android:layout_marginRight="14dp"
        android:src="@drawable/test" />

    <EditText
        android:id="@+id/messageEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/sendImageButton"
        android:ems="10"
        android:gravity="top"
        android:hint="@string/messageEditText"
        android:inputType="textMultiLine"
        android:minHeight="55dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />

    <ImageButton
        android:id="@+id/sendImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/messageEditText"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/messageEditText"
        android:adjustViewBounds="true"
        android:maxHeight="55dp"
        android:maxWidth="55dp"
        android:padding="12dp"
        android:scaleType="centerInside"
        android:src="@drawable/sendmessage" />

</RelativeLayout>

Half of the EditText is hidden. I also can't scroll listview. Any solution?

回答1:

Solution was to set an android:softInputMode attribute to adjustResize in Manifest and put layout(not list item layout) inside a ScrollView.



回答2:

I Had a similar issue, then i used android:windowSoftInputMode="adjustResize|stateHidden" as mentioned here.. It works very well for me..



回答3:

From my early days as a Android Developer I struggled with the virtual keyboard. I am amazed that Android is still not giving an elegant and clear solution for this.

So here is the Workaround that will put this mess behind you. it will work without the ScrollView workaround or giving away your full screen flag.

  1. Add this awesome Library to your gradle file:

compile'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:1.0.1'

  1. Make sure your activity has the following keyboard settings:

android:windowSoftInputMode="adjustResize"

  1. wrap your EditText with a vertical LinearLayout and add a View with Visibility of Gone:

    <com.ylimitapp.ylimitadmin.views.NormalFontEditText
        android:id="@+id/input_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapSentences|textMultiLine"
        android:imeOptions="actionSend"
        android:paddingStart="20dp"
        android:paddingEnd="40dp"
        android:paddingTop="10dp"
        android:maxHeight="120dp"
        android:adjustViewBounds= "true"
        android:scrollHorizontally="false"
        android:textColorHint="#7b7b7b"
        android:hint="@string/type_your_message"
        android:background="@drawable/msg_inputfield_bg"
        android:textColor="@color/black_text_color"
        android:textSize="15.33sp"
        />
    
        <View
            android:id="@+id/keyboard_view"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:visibility="gone"
            />
    
    </LinearLayout>
    
  2. calculate the screen size so you can calculate the height of the view that pushes the EditText:

    private void storeScreenHeightForKeyboardHeightCalculations() {
                Rect r = new Rect();
                View rootview = getActivity().getWindow().getDecorView(); 
                rootview.getWindowVisibleDisplayFrame(r);
                mOriginalScreenHeight = r.height();
    
                Rect rectangle = new Rect();
                Window window = getActivity().getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
                int statusBarHeight = rectangle.top;
                int contentViewTop =
                        window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                int titleBarHeight= contentViewTop - statusBarHeight;
    
                if (titleBarHeight == 0) {
                    mOriginalScreenHeight -= (24 * Utils.getDensity(getContext()));
                }
            }
    
    1. Add a Listener for keyboard open and close event and then set the height of the View below the EditText on runtime so we will set the height properly on any device and custom keyboards. then simply make it Visible when the Keyboard is Open:
private void addkeyBoardlistener() {
           KeyboardVisibilityEvent.setEventListener(
                            getActivity(),
                            new KeyboardVisibilityEventListener() {
                                @Override
                                public void onVisibilityChanged(boolean isOpen) {
                                    if (isOpen) {
                                        Rect r = new Rect();
                                        View rootview = getActivity().getWindow().getDecorView(); // this = activity
                                        rootview.getWindowVisibleDisplayFrame(r);
                                        int keyboardHeight = (mOriginalScreenHeight - r.height());
                                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) keyboard_view.getLayoutParams();
                                        params.height = (int) ((keyboardHeight + 5 * Utils.getDensity(getContext())));
                                        keyboard_view.setLayoutParams(params);
                                        keyboard_view.setVisibility(View.VISIBLE);
                                    } else {
                                        keyboard_view.setVisibility(View.GONE);
                                    }
                                }
                            });
                }

This is the result:



回答4:

Disable the keyboard on window startup

this .getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN );


回答5:

Set an android:softInputMode attribute for your Activity element in your Manifest.

See the Manifest documentation for a full list of valid values and their effect. The ones of particular interest to you will likely be adjustPan and adjustResize.