【问题标题】:Android TextInputLayout icon in error (or hint) text错误(或提示)文本中的 Android TextInputLayout 图标
【发布时间】:2018-01-16 15:32:25
【问题描述】:

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

我的代码

在我的活动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

I found an implementation 使用 setError(text, drawable) 但我使用 KotlinsetError 不可用。

所以我尝试了:

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"

但这不会渲染drawable,只是资源路径的字符串。

尝试 2

我发现another one 覆盖了TextInputLayoutHelper,以便在文本旁边设置一个drawable。如您所见,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 方式”将此图标添加到错误/提示文本旁边?

【问题讨论】:

  • 把所有的edittext放在一个框架布局中并在它上面放一个视图怎么样?
  • @UrielFrankel 对于这样的实现,我唯一的犹豫是我想通过使用 app:errorEnabledapp:errorTextAppearance 来控制样式/位置来坚持“原生 Android”的做事方式我的错误。每当我想使用图标时,我都不想担心定位我的错误文本。但这绝对是解决我遇到的这个问题的有效方法,谢谢!

标签: android kotlin android-drawable


【解决方案1】:

您可以像这样将 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)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你想在TextInputLayout的错误容器内放置一个图标

      如果你检查真实的布局

      你会发现你的 textview 甚至是包装内容。

      SDK 源代码中也有

      if (enabled) {
          mErrorView = new AppCompatTextView(getContext());
          mErrorView.setId(R.id.textinput_error);
          ...
      

      所以没有地方可以按原样在右侧插入图标。

      • 一种可能的解决方案是以编程方式在布局中找到此视图,更改宽度并应用可绘制对象。
      • 第二种(可能也是最简单的)解决方案是实现您自己的简单错误布局,而不是为TextInputLayout 设置错误,而是自己管理错误状态

      【讨论】:

        猜你喜欢
        • 2010-09-22
        • 1970-01-01
        • 2021-12-16
        • 2023-03-17
        • 1970-01-01
        • 2018-03-21
        • 2023-03-21
        • 2017-03-06
        • 2018-11-02
        相关资源
        最近更新 更多