【问题标题】:Error message spacing not working in TextInputLayout Outlined style错误消息间距在 TextInputLayout 大纲样式中不起作用
【发布时间】:2020-08-09 06:34:18
【问题描述】:

我正在尝试使用 MaterialComponents TextInputLayout 实现一个表单。根据docs,它应该有一个错误信息的空间。我在布局 xml 中启用了错误消息,它也显示在 Android Studio 布局预览中,但在应用程序中不起作用。

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/name_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:hint="@string/hint_name">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/input_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimaryText"
            android:singleLine="true" />

    </com.google.android.material.textfield.TextInputLayout>

预览:

实际发生了什么:

【问题讨论】:

  • 您使用的是什么版本的 Material Design Components?
  • @SaurabhThorat 'com.google.android.material:material:1.1.0'
  • 尝试使用 1.2.0-alpha06。此版本中对错误行为进行了一些修复。 1.1.0 对我来说效果很好。
  • 这只是小部件的默认行为。如果您希望预览空间始终被占用且可见,您可以尝试设置帮助文本。
  • 我找到了解决方案。

标签: android android-layout material-design android-textinputlayout


【解决方案1】:

我通过在输入有效时禁用 errorMessage 后立即启用它来修复它。

    else {  // Condition finally met when error is valid
        signinEmailLayout.setErrorEnabled(false);
        signinEmailLayout.setErrorEnabled(true);
    }

正如 @MRamzan 在 cmets 中所建议的,这似乎是默认行为。无论如何,在我的活动类中,我将 errorEnabled 设置为 false,这会导致布局在输入有效时删除为错误消息指定的空间。一旦输入再次无效,错误消息就会被添加,因为我再次启用它,因此不稳定的用户体验就在那里。

【讨论】:

    【解决方案2】:

    设置app:errorEnabled="true" 将修改布局以留出空间让错误文本出现。问题是您正在设置 errorEnabled=false 以隐藏错误,从而删除错误文本的空间。

    而是始终保留errorEnabled=true 并使用TextInputLayout#error 来显示和隐藏错误。

    // Shows error text and UI
    nameInputLayout.error = getString(R.string.error)
    
    // Hides error text and UI
    nameInputLayout.error = null
    

    请参阅“向文本字段添加错误”下的 https://material.io/components/text-fields/android#using-text-fields

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 2021-03-07
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多