Android:TextInputEditText 的底部总是滚动到软键盘的顶部

时间:2021-02-26 16:11:50

标签: android material-components-android android-textinputlayout android-nestedscrollview android-textinputedittext

我在 TextInputLayout 中使用 TextInputEditText,它位于 NestedScrollView 中:

<androidx.core.widget.NestedScrollView
    android:id="@+id/nsv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textField"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@{hint}"
            android:padding="@dimen/padding_items"
            app:startIconContentDescription="@{iconDescription}"
            app:startIconDrawable="@{icon}">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:importantForAutofill="no"
                android:text="@={text}" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

当我输入长文本时,TextInputEditText 占用的垂直空间大于软键盘上方的可用空间,TextInputEditText 的上部滚动到视图之外。 这完全没问题。

但是如果我手动向上滚动nestedScrollView以便我可以编辑TextInputEditText的第一个单词,它会一直向后滚动并且每当我单击(=设置光标)进入TextInputEditText或输入一个字母时就会发生这种情况。< /p>

每次与软键盘交互时,TextInputEditText 的底部边缘似乎都与软键盘的顶部边缘对齐,这就是为什么即使将光标放在最开始时顶部边缘也会滚动出视图的原因。

这使得无法编辑长字符串的开头,因为您必须在每个字母之后向上滚动查看会发生什么,否则您必须盲目输入。

我该如何解决这个问题?

重现步骤:

  1. 输入长文本
  2. 滚动到文本的开头
  3. 将光标置于 TextInputEditText 的开头
  4. 再次滚动到文本的开头,使光标可见: cursor at the beginning of the TextInputEditText
  5. 输入字母: cursor no longer visible, because scrolled out of view

1 个答案:

答案 0 :(得分:0)

在清单文件的活动标签中添加这一行:

android:windowSoftInputMode="adjustPan"

此外,您可以在 XML 上的 TextInputEditText 中对光标进行着色和手动图像。

相关问题