自定义 EditText kotlin 的 setError 图标
Customizing the setError icon for EditText kotlin
我正在使用 TextInputLayout。我试试这个,但没有显示图标。帮助我在 TextInputLayout 中看到 errorIcon。
我想为我的 EditText 设置一个 setError 方法,使用自定义图标而不是默认的 Android 图标。所以我尝试了这个:
提前致谢
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilFirstName"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/civProfileImage"
android:layout_marginStart="@dimen/margin_twenty_four"
android:layout_marginTop="40dp"
android:layout_marginEnd="@dimen/margin_twenty_four"
android:layout_marginBottom="@dimen/margin_sixteen"
android:hint="@string/first_name"
android:textColorHint="@color/text_input_layout_hint_color"
android:theme="@style/TextInputLayoutStyle"
app:boxCornerRadiusBottomEnd="5dp"
app:boxCornerRadiusBottomStart="5dp"
app:boxCornerRadiusTopEnd="5dp"
app:boxCornerRadiusTopStart="5dp"
app:boxStrokeColor="@color/text_input_layout_stroke_color"
app:boxStrokeErrorColor="@color/colorRed"
app:errorTextColor="@color/colorRed"
app:hintEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtFirstlName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#!$%"
android:enabled="false"
android:focusable="false"
android:fontFamily="@font/roboto_regular"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:textColor="@color/colorTextNormal"
android:textSize="@dimen/text_size_sixteen" />
</com.google.android.material.textfield.TextInputLayout>
val drawable = resources.getDrawable(R.drawable.ic_baseline_error) as Drawable
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
tilFirstName.error = getString("Error", Drawable)
尝试在 xml 文件中使用 app:errorIconDrawable
或在 kotlin 文件中对 TextInputLayout 使用 setErrorIconDrawable(Drawable)
。
在TextInputLayout
中使用setErrorIconDrawable
方法:
tilFirstName.error = "Error text!!";
tilFirstName.setErrorIconDrawable(R.drawable.xxxx);
我正在使用 TextInputLayout。我试试这个,但没有显示图标。帮助我在 TextInputLayout 中看到 errorIcon。
我想为我的 EditText 设置一个 setError 方法,使用自定义图标而不是默认的 Android 图标。所以我尝试了这个:
提前致谢
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilFirstName"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/civProfileImage"
android:layout_marginStart="@dimen/margin_twenty_four"
android:layout_marginTop="40dp"
android:layout_marginEnd="@dimen/margin_twenty_four"
android:layout_marginBottom="@dimen/margin_sixteen"
android:hint="@string/first_name"
android:textColorHint="@color/text_input_layout_hint_color"
android:theme="@style/TextInputLayoutStyle"
app:boxCornerRadiusBottomEnd="5dp"
app:boxCornerRadiusBottomStart="5dp"
app:boxCornerRadiusTopEnd="5dp"
app:boxCornerRadiusTopStart="5dp"
app:boxStrokeColor="@color/text_input_layout_stroke_color"
app:boxStrokeErrorColor="@color/colorRed"
app:errorTextColor="@color/colorRed"
app:hintEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtFirstlName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#!$%"
android:enabled="false"
android:focusable="false"
android:fontFamily="@font/roboto_regular"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:textColor="@color/colorTextNormal"
android:textSize="@dimen/text_size_sixteen" />
</com.google.android.material.textfield.TextInputLayout>
val drawable = resources.getDrawable(R.drawable.ic_baseline_error) as Drawable
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
tilFirstName.error = getString("Error", Drawable)
尝试在 xml 文件中使用 app:errorIconDrawable
或在 kotlin 文件中对 TextInputLayout 使用 setErrorIconDrawable(Drawable)
。
在TextInputLayout
中使用setErrorIconDrawable
方法:
tilFirstName.error = "Error text!!";
tilFirstName.setErrorIconDrawable(R.drawable.xxxx);