我有一个TextInputLayout
里面有一个TextInputEditText
。这很好地工作,特别是为了显示错误(当我需要在文本字段上显示错误时,我只调用setError("error message")
并且错误消息显示在EditText下)。
问题
问题在于键盘通常会隐藏此错误,尤其是在必须平移屏幕以显示EditText的情况下。在下面的屏幕截图中查看我的意思:
看看如何在下部区域聚焦时隐藏错误消息?我必须手动向下滚动:
题
当字段聚焦时,如何告诉框架平移到TextInputLayout
的底部而不是TextInputEditText
的底部?
供参考,这是我的布局。
<android.support.constraint.ConstraintLayout
android:id="@+id/clearable_edittext_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/clearable_edit_text_height"
android:background="@drawable/clearable_edit_text_background">
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="6dp"
android:layout_marginStart="@dimen/large_padding"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:labelFor="@id/clearable_text"
app:errorEnabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/clear_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/clearable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:inputType="none"
android:saveEnabled="false"
android:singleLine="true"
android:textColorHint="@color/new_grey_2"
android:textSize="@dimen/font_18"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:contentDescription="@string/default_string"
android:paddingEnd="@dimen/large_padding"
android:paddingStart="@dimen/small_padding"
android:src="@drawable/icn_clear"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
最佳答案
<android.support.constraint.ConstraintLayout
android:id="@+id/clearable_edittext_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:labelFor="@+id/clearable_text"
app:errorEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/clearable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:inputType="none"
android:saveEnabled="false"
android:singleLine="true"
android:textColorHint="#DFDFDF"
android:textSize="18dp"
android:text="Hello world"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/clear_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_team"
app:layout_constraintTop_toTopOf= "@+id/text_input_layout"
app:layout_constraintBottom_toBottomOf="@+id/text_input_layout"
app:layout_constraintLeft_toRightOf="@+id/text_input_layout"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
关于android - 在TextInputEditText中使用时,软键盘会隐藏TextInputLayout的一部分。如何调整平移?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48351843/