为了遵循浮动标签输入的材料指南,我使用了TextInputLayoutHelper
小部件。它目前看起来是这样的:
我的代码
在我的activities onCreate
函数中,我有:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val passwordInputLayout = this.findViewById<TextInputLayoutHelper>(R.id.input_layout_password)
passwordInputLayout.error = "8+ characters and at least one uppercase letter, a number, and a special character (\$, #, !)"
passwordInputLayout.isErrorEnabled = true
}
我xml
里的小工具看起来像..。
<TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditTextTheme"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorAppearance"
app:passwordToggleDrawable="@drawable/asl_password_visibility"
app:passwordToggleEnabled="true"
app:passwordToggleTint="?colorControlNormal">
<EditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/set_a_password"
android:inputType="textPassword"
android:singleLine="true" />
</TextInputLayout>
我想做的是
我想在错误/提示文本(感叹号三角形)中错误文本的右侧放置一个图标。
我的尝试
尝试1
使用setError(text, drawable)
的I found an implementation不可用,但我使用的是Kotlin
to setError
。
所以我试着:
val warningIcon = ResourcesCompat.getDrawable(resources, R.drawable.ic_warning_black_24dp, null)
warningIcon?.setBounds(0, 0, warningIcon.intrinsicWidth, warningIcon.intrinsicHeight)
passwordInputLayout.error = "8+ characters and at least one uppercase letter, a number, and a special character (\$, #, !) $warningIcon"
但这并不会呈现可绘制的内容,而仅仅是资源路径的字符串。
尝试2
我发现覆盖TextInputLayoutHelper
的another one是为了在文本旁边设置一个可绘制的。如您所见,setError
只包含接口override fun setError(error: CharSequence?)
,该接口没有drawable
参数。
override fun setError(error: CharSequence?) {
super.setError(error)
val warningIcon = ResourcesCompat.getDrawable(resources, R.drawable.ic_warning_black_24dp, null)
warningIcon?.setBounds(0, 0, warningIcon.intrinsicWidth, warningIcon.intrinsicHeight)
// mHelperView is a TextView used in my custom `TextInputLayout` override widget
mHelperView!!.setCompoundDrawables(null, null, warningIcon, null)
}
是否有覆盖或内置的"Android方式“来在错误/提示文本旁边添加此图标?
发布于 2018-08-02 19:20:59
可以将SpannableString与ImageSpan一起使用,如下所示
val errorDrawable = ContextCompat.getDrawable(context!!, R.drawable.ic_error)
your_text_input_layout.error = SpannableString("your string").apply {
setSpan(ImageSpan(errorDrawable, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
发布于 2018-02-02 09:00:34
这可能会帮助其他陷入类似麻烦的人。我看了源代码。问题是setError(text, drawable)
是在EditText类中定义的,而不是您的布局所基于的TextInputLayout
。
我和你有类似的代码和问题,因为TextInputLayout没有任何方法来替换Drawable,所以我们必须在TextInputLayout下面创建一个TextView来模拟错误消息。
https://stackoverflow.com/questions/48284757
复制相似问题