【问题标题】:Soft Keyboard pushed button up above it. How to fix it?软键盘按了上面的按钮。如何解决?
【发布时间】:2019-05-18 16:56:31
【问题描述】:

我有带有 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>

【问题讨论】:

  • 只需在清单文件中的 activity 标签中使用 android:windowSoftInputMode="adjustNothing" 即可
  • 但是如果我改变它就可以滚动到 ScrollView 的末尾?我当然会马上试试。
  • 用问题分享你的 xml 布局
  • 我添加了xml示例

标签: android keyboard window-soft-input-mode


【解决方案1】:

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

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

android:windowSoftInputMode="adjustPan|stateHidden"

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

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

让我知道这是否有效。

【讨论】:

  • 我已经编辑了答案以实现您的目标,希望对您有所帮助!
  • 好的,我会在 1 小时内尝试。
  • 当然,希望这会有所帮助!
  • 它可以工作,但滚动视图内容位于键盘后面,就像滚动视图底部附加到屏幕底部一样。如果有更多内容,它可以滚动,但最后一部分等于键盘高度是隐藏的。但我认为需要更好的解决方案。我考虑在键盘显示上仅隐藏提交按钮,但 Android 没有本机键盘显示/隐藏侦听器,只是一些解决方法
  • 不是最理想的方法,但是,您可以做的是计算键盘的高度(已在答案中添加了代码)并在视图底部使用该高度即使在键盘打开后它也会滚动到底部视图。
猜你喜欢
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
相关资源
最近更新 更多