如何移除TextInputEditText点击后显示的左上角悬浮提示?
问题解决方法
你遇到的是Material Design TextInputLayout组件的默认浮动提示特性,按以下步骤修改即可禁用提示上浮效果,仅保留输入框未输入时的原生提示:
- 第一步:修改TextInputLayout配置
将TextInputLayout的app:hintEnabled属性值改为false,删除TextInputLayout上设置的android:hint属性,同时把原本写在TextInputLayout上的输入相关属性(imeOptions、inputType、行数限制等)迁移到内部的TextInputEditText中,这类属性本就属于输入框控件。 - 第二步:给TextInputEditText添加提示
将提示文案设置到TextInputEditText的android:hint属性中。 - 可选优化:删除TextInputEditText的
android:background="#FFFFFF"属性,该属性会覆盖OutlinedBox样式自带的边框效果,导致输入框外框消失,如果需要白色背景,直接给TextInputLayout设置background属性即可。
修改后的完整布局代码如下:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/rc_message_activity" android:layout_width="match_parent" android:layout_height="0dp" android:elevation="2dp" app:layout_constraintBottom_toTopOf="@+id/tv_msg" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:listitem="@layout/item_message" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/tv_msg" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="365dp" android:layout_height="wrap_content" android:elevation="22dp" android:padding="0dp" app:hintEnabled="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/floatingActionButton" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="This is my cool hint" android:imeOptions="actionSend" android:inputType="textMultiLine" android:lines="5" android:maxLines="10" android:minLines="1" android:textColor="#000000" android:textSize="20sp" /> </com.google.android.material.textfield.TextInputLayout>
内容的提问来源于stack exchange,提问作者Oğuz Recep Özdemir
相关产品推荐
相关产品推荐

