【问题标题】:How to show the error icon in a TextInputLayout programatically?如何以编程方式在 TextInputLayout 中显示错误图标?
【发布时间】:2020-11-15 08:46:32
【问题描述】:

我正在使用 TextInputLayout 小部件,我只想在视图的末尾显示错误图标,而不考虑错误消息。

【问题讨论】:

    标签: android android-edittext android-textinputlayout textinputlayout


    【解决方案1】:

    如果您想更改错误图标并显示错误,请使用类似

    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)
    

    【讨论】:

      【解决方案2】:

      如果你想显示错误图标,唯一的内置方法是显示消息错误:

      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));
      

      【讨论】:

      • 不工作.. !! “setEndIconMode”和“setEndIconDrawable”函数不存在了!
      • @J.M.J 他们在1.1.01.2.0-rc011.3.0-alpha02存在。你用的是哪个版本?
      【解决方案3】:

      为此请使用以下kotlin

      yourInputLayout.error = "Please enter valid email address"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-03
        • 1970-01-01
        相关资源
        最近更新 更多