软键盘向上推隐藏动作栏或叠加编辑文本

时间:2015-12-10 14:19:52

标签: android android-layout android-softkeyboard soft-keyboard

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

runOnUIThread()
发生这种情况: Bug

在任何情况下,只有当我从横向打开键盘然后旋转(键盘打开)时才会发生这种情况。 在正常使用中(旋转然后打开键盘),这不会发生!

如果我设置

android:windowSoftInputMode="stateHidden|adjustResize"

ActionBar被推高。

这里布局代码:

android:windowSoftInputMode="stateHidden|adjustPan"

FIX: 问题出在mikepenz MaterialDrawer上。 现在我联系了迈克,我们修复了错误

2 个答案:

答案 0 :(得分:1)

SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 标志出现问题时,需要显示StatusBar后面的抽屉。

作为解决方案,我实现了KeyboardUtil,它将键盘的高度添加为内容视图的填充。由于在显示键盘时没有听众可以获得事件,因此可以通过OnGlobalLayoutListener完成。

@MarcoCount有一个特殊用例导致KeyboardUtil无法正常工作。在他的帮助下,我们得到了KeyboardUtil的更新版本,现在也可以在屏幕旋转时工作,键盘会一直显示。

如果有人需要,这是更新KeyboardUtil的来源。它也可以在source of the MaterialDrawer

中的GitHub上找到
/*
 * 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);
        }
    }
}

答案 1 :(得分:1)

尝试在布局中使用滚动视图,而不是在清单文件中添加adjustpan。 使用滚动视图,您的屏幕是可调节和可滚动的,这不会向上滚动您的工具栏或操作栏,也不会隐藏您的布局。

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

尝试将此布局用作父布局。但滚动视图不能用作基本布局,您必须在线性或相对布局中使用scrollView。

相关问题