【发布时间】:2021-05-29 00:48:58
【问题描述】:
我在 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 或输入一个字母时就会发生这种情况。
每次我与之交互时,TextInputEditText 的底部边缘似乎都与软键盘的顶部边缘对齐,这就是即使光标放在最开始时顶部边缘也会滚动到视野之外的原因。
这使得编辑长字符串的开头变得不可能,因为您必须在每个字母之后向上滚动并查看会发生什么,或者您必须盲目输入。
我该如何解决这个问题?
重现步骤:
- 输入长文本
- 滚动到文本的开头
- 将光标放在 TextInputEditText 的开头
- 再次滚动到文本的开头,使光标可见: cursor at the beginning of the TextInputEditText
- 输入字母: cursor no longer visible, because scrolled out of view
【问题讨论】:
-
尝试使用简单的滚动视图而不是嵌套。
-
很遗憾没用;使用 ScrollView 而不是 NestedScrollView 会导致相同的行为
标签: android material-components-android android-textinputlayout android-nestedscrollview android-textinputedittext