Soft keyboard push up hidding action bar or overlay edittext

Deadly 提交于 2020-01-01 19:46:07

问题


I have an issue with a simple layout. If I set

android:windowSoftInputMode="stateHidden|adjustResize"

this happens: Bug

In any case this happens only If I open keyboard from landscape, then rotate (with keyboard open). In normal use (rotating and then opening keyboard) this does not happens!

If I set

android:windowSoftInputMode="stateHidden|adjustPan"

ActionBar is pushed up.

Here layout code:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_below="@+id/ad_view_chat"
        android:layout_gravity="bottom"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listChatLayout"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_above="@+id/switchbuttons"
            android:layout_marginTop="2dp">
            <ListView
                android:id="@+id/listChat"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:longClickable="true"
                android:stackFromBottom="true"
                android:transcriptMode="normal" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/switchbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_above="@+id/messaggiLayout"
            android:layout_centerHorizontal="true">
            <RadioButton
                android:id="@+id/cmd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:checked="false"
                android:gravity="center"
                android:text="Cmd"
                android:layout_marginLeft="5dp" />

            <RadioButton
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_toRightOf="@+id/cmd"
                android:checked="true"
                android:gravity="center"
                android:text="Message" />

            <com.rey.material.widget.Button
                android:id="@+id/button_arrow_down"
                android:background = "@drawable/arrow_down"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button_arrow_up"
                android:layout_toEndOf="@+id/button_arrow_up"
                android:layout_marginLeft="3dp" />

            <com.rey.material.widget.Button
                android:id="@+id/button_arrow_up"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background = "@drawable/arrow_up"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/message"
                android:layout_toEndOf="@+id/message"
                android:layout_marginLeft="20dp" />
        </RelativeLayout>


        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/listChatLayout"
            android:layout_alignParentBottom="true"
            android:id="@+id/messaggiLayout">
            <EditText
                android:id="@+id/messaggi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:imeOptions="actionSend|flagNoFullscreen"
                android:inputType="textImeMultiLine"
                android:layout_toLeftOf="@+id/button_send"
                android:maxLength="100">
                <requestFocus />
            </EditText>
            <com.rey.material.widget.Button
                android:id="@+id/button_send"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="50dp"
                android:layout_alignParentRight="true"
                android:background = "@drawable/send2"
                android:layout_height="50dp"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3" />
        </RelativeLayout>
    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view_chat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_chat_ad_unit_id" />
</RelativeLayout>

FIX: The problem was in mikepenz MaterialDrawer. Now I contacted mike and we fixed the bug


回答1:


The problem occurs when the SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flag, which is required to display the drawer behind the StatusBar.

As a solution for this I implemented the KeyboardUtil which adds the height of the keyboard as padding to the contentview. As there is no listener to get an event when the keyboard is shown, this is done via a OnGlobalLayoutListener.

@MarcoCount had a special usecase which caused the KeyboardUtil to not working correctly. With the help of him we came to an updated version of the KeyboardUtil which now also works when the screen is rotated, and the Keyboard keeps showing the whole time.

Here's the source of the updated KeyboardUtil in case someone needs it. It is also available on GitHub within the source of the MaterialDrawer


/*
 * Copyright 2015 Mike Penz All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.materialdrawer.util;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by mikepenz on 14.03.15.
 * This class implements a hack to change the layout padding on bottom if the keyboard is shown
 * to allow long lists with editTextViews
 * Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };


    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }
}



回答2:


try to use scroll view in your layout instead of adding adjustpan at manifest file. Using the scroll view your screen is adjustable and scrollable and this will not scroll up your toolbar or action bar and not gonna to hide your layout.

<ScrollView>
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:fillViewport="true"
 android:scrollbars = "vertical"
 android:scrollbarStyle="insideInset"
> </ScrollView>

try using this layout as your parent layout. But scroll view cant be used as the base layout for that you have to use the scrollView inside a linear or relative layout.



来源:https://stackoverflow.com/questions/34204230/soft-keyboard-push-up-hidding-action-bar-or-overlay-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!