更新答案(添加更多信息后)
尝试在
TextInputLayout
而不是 TextInputEditText
上调用 setError()。然后,视图本身将确保错误图标不会与结束图标重叠。
不要将编辑图标放置为背景可绘制对象,尝试将其放置为
drawableEnd
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/edit_text"
android:inputType="text" />
更新答案
实际上,我正在遵循下面的 XML 代码
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/enter_mail_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dimen_20dp"
android:layout_marginEnd="@dimen/dimen_20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/mail"
style="@style/TextViewStyleRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:hint="@string/enter_mail_2"
android:inputType="textEmailAddress|textNoSuggestions"
android:maxLength="50" />
</com.google.android.material.textfield.TextInputLayout>
Kotlin 代码
binding.enterMailText.setErrorIconDrawable(R.drawable.no_icon)
binding.enterMailText.error = "Please enter valid email address"
no_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
这对我有用:
绑定.yourInputLayout.errorIconDrawable = null