【发布时间】:2020-11-15 08:46:32
【问题描述】:
我正在使用 TextInputLayout 小部件,我只想在视图的末尾显示错误图标,而不考虑错误消息。
【问题讨论】:
标签: android android-edittext android-textinputlayout textinputlayout
我正在使用 TextInputLayout 小部件,我只想在视图的末尾显示错误图标,而不考虑错误消息。
【问题讨论】:
标签: android android-edittext android-textinputlayout textinputlayout
如果您想更改错误图标并显示错误,请使用类似
val errorDrawable = ContextCompat.getDrawable(context!!, R.drawable.ic_error)
input_layout.error = SpannableString("error message").apply {
setSpan(ImageSpan(errorDrawable, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
【讨论】:
如果你想显示错误图标,唯一的内置方法是显示消息错误:
textInputLayout.setError("Error message");
如果您想在不显示错误消息的情况下显示错误图标,有一个解决方法:
//Update the EndIcon with a custom icon error
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
textInputLayout.setEndIconDrawable(R.drawable.error_icon);
//Tint the end icon with the error color defined in your app theme
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorError, typedValue, true);
textInputLayout.setEndIconTintList(
ContextCompat.getColorStateList(this,typedValue.resourceId));
【讨论】:
为此请使用以下kotlin 行
yourInputLayout.error = "Please enter valid email address"
【讨论】: