首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >错误(或提示)文本中的安卓TextInputLayout图标

错误(或提示)文本中的安卓TextInputLayout图标
EN

Stack Overflow用户
提问于 2018-01-16 23:32:25
回答 2查看 7.2K关注 0票数 10

为了遵循浮动标签输入的材料指南,我使用了TextInputLayoutHelper小部件。它目前看起来是这样的:

我的代码

在我的activities onCreate函数中,我有:

代码语言:javascript
复制
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里的小工具看起来像..。

代码语言:javascript
复制
<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

所以我试着:

代码语言:javascript
复制
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

我发现覆盖TextInputLayoutHelperanother one是为了在文本旁边设置一个可绘制的。如您所见,setError只包含接口override fun setError(error: CharSequence?),该接口没有drawable参数。

代码语言:javascript
复制
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方式“来在错误/提示文本旁边添加此图标?

EN

回答 2

Stack Overflow用户

发布于 2018-08-02 19:20:59

可以将SpannableString与ImageSpan一起使用,如下所示

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2018-02-02 09:00:34

这可能会帮助其他陷入类似麻烦的人。我看了源代码。问题是setError(text, drawable)是在EditText类中定义的,而不是您的布局所基于的TextInputLayout

我和你有类似的代码和问题,因为TextInputLayout没有任何方法来替换Drawable,所以我们必须在TextInputLayout下面创建一个TextView来模拟错误消息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48284757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档