软键盘推入隐藏操作栏或覆盖edittext [英] Soft keyboard push up hidding action bar or overlay edittext

查看:107
本文介绍了软键盘推入隐藏操作栏或覆盖edittext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的布局问题. 如果我设置

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

android:windowSoftInputMode="stateHidden|adjustResize"

发生这种情况: 错误

无论如何,仅当我从横向打开键盘时,然后旋转(在键盘打开的情况下),这种情况才会发生. 在正常使用情况下(旋转然后再打开键盘)不会发生这种情况!

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!

如果我设置

android:windowSoftInputMode="stateHidden|adjustPan"

ActionBar被推上.

ActionBar is pushed up.

此处的布局代码:

    <?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: 问题出在mikepenz MaterialDrawer中. 现在,我联系了迈克,我们已修复了该错误

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

推荐答案

SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 标志出现时,会出现此问题,该标志是显示StatusBar后面的抽屉所必需的.

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

作为解决方案,我实现了KeyboardUtil,它将键盘的高度作为填充添加到contentview中.由于显示键盘时没有侦听器获取事件,因此可以通过OnGlobalLayoutListener完成.

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有一个特殊的用例,导致KeyboardUtil无法正常工作.在他的帮助下,我们来到了KeyboardUtil的更新版本,该版本现在在旋转屏幕时也可以使用,并且键盘一直在显示整个时间.

@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.

以下是更新的KeyboardUtil的来源,以防有人需要.它也可以在GitHub的 MaterialDrawer的来源

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);
        }
    }
}

这篇关于软键盘推入隐藏操作栏或覆盖edittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆