0

我有带有 ScorllView 和 Button 的 ConstraintLayout(连接到屏幕底部。当我在 ScrollView 内编辑 EditText 输入时。然后出现的键盘正在向上移动我的 ScrollView 内容(所需的行为,所以我可以滚动到它的末尾)但是它还按下按钮(不良行为)。

我想我可以改变 windowAdjustMode,也许我可以检测到键盘显示然后隐藏这个按钮?但这两种解决方案并不完美。在此处输入图像描述

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>
4

1 回答 1

1

这可能会有所帮助,我自己没有尝试过,请尝试将以下代码添加到activity清单中的标记中

编辑 - 添加stateHidden以实现您要查找的内容,按钮将位于底部,并且可以滚动滚动视图内的元素。

android:windowSoftInputMode="adjustPan|stateHidden"

来自Android 文档- adjustPan- 活动的主窗口未调整大小以为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。

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

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通过以编程方式创建视图并设置它的高度来添加它。

编辑 3 -

用它来隐藏键盘

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

让我知道这个是否奏效。

于 2018-12-18T09:55:05.960 回答