Android:如何防止软键盘隐藏任何视图?

时间:2016-06-20 03:04:34

标签: android view hidden soft-keyboard

我的布局中有4行输入字段 问题是,当我点击最上面的EditText时,会出现软键盘并隐藏最下面的EditText
我的目标是在弹出软键盘时保持每个EditText字段在屏幕上可见。

我在Manifest中尝试windowSoftInputMode,但这不是我想要的,它只是让我们专注于#39;输入字段可见。

是否有View类型的标志以防止从软键盘中隐藏,以便屏幕始终滚动显示指定的视图?

1 个答案:

答案 0 :(得分:1)

您可以将4行输入字段放入linearlayout视图,然后将此linearlayout放入滚动视图,当出现软键盘时,将滚动视图滚动到最后的EditText视图位置。

int[] loc = new int[2];

lastEditTextView.getLocationOnScreen(loc);

检测键盘的方法出现:

private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        Rect r = new Rect();
        if (activityRootView == null) {
            return;
        }
        activityRootView.getWindowVisibleDisplayFrame(r);
        int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);

        if (heightDiff > 100) {
            // keyboard show
            scrollView.smoothScrollTo(loc[0],loc[1]);
        } else {
            // keyboard hidden
        }
    }
};

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);