软键盘将其上方的按钮向上推.如何解决? [英] Soft Keyboard pushed button up above it. How to fix it?

查看:100
本文介绍了软键盘将其上方的按钮向上推.如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ConstraintLayout,其下方带有ScorllView和Button(附加到屏幕底部.当我在ScrollView中编辑EditText输入时.然后出现的键盘将ScrollView内容向上移动(期望的行为,因此我可以滚动到末尾)),但也可以将按钮向上推(不良行为).

I have ConstraintLayout with ScorllView and Button below it (attached to bottom of the screen. When I am editing EditText input inside ScrollView. Then appearing keyboard is moving my ScrollView content up (desired behaviour, so I can scroll to the end of it) but it also pushing button up (undesired behaviour).

我想我可以更改windowAdjustMode,也许我可以检测到键盘显示然后隐藏此按钮?但是这两个解决方案都不是完美的.

I think I can change windowAdjustMode, maybe I could detect keyboard showing and then hide this button? But this two solutions aren't perfect.

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/submitButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="0dp">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <EditText /> goes here 
    </android.support.constraint.ConstraintLayout>
</ScrollView>
    <Button
        android:id="@+id/submitButton"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="0dp"
        android:text="@string/wizard_singup_step_submit_button"
        style="@style/FormSubmitButton"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

推荐答案

这可能有帮助,我自己还没有尝试过,请尝试将以下代码添加到清单

This might help, haven't tried it myself, try adding the below code to your activity tag inside your manifest

编辑-添加了 stateHidden 以实现所需的功能,该按钮位于底部,并且可以滚动滚动视图中的元素.

Edit - added stateHidden to achieve what you're looking for, the button will be at the bottom and the elements inside the scroll view can be scrolled.

android:windowSoftInputMode="adjustPan|stateHidden"

来自 Android文档- adjustPan -活动的主窗口未调整大小以为软键盘腾出空间.而是,窗口的内容会自动平移,以使当前焦点不会被键盘遮挡,并且用户始终可以看到他们正在键入的内容.通常,这不如调整大小那么可取,因为用户可能需要关闭软键盘才能进入并与模糊的窗口交互.

From Android Documentation - adjustPan - The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

编辑2-用于计算键盘高度的代码

Edit 2 - Code for calculating the height of the Keyboard

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    Log.d("Keyboard Size", "Size: " + heightDifference);

                }
            });

通过编程方式创建视图并设置其高度,以添加 heightDifference .

Add that heightDifference by creating a View Programmatically and setting it's height.

编辑3-

使用此按钮隐藏键盘

public static void hideKeyboardFrom(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

让我知道这是否可行.

这篇关于软键盘将其上方的按钮向上推.如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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