【问题标题】:Android TextInputLayout is not showing errorAndroid TextInputLayout 没有显示错误
【发布时间】:2015-12-17 09:46:56
【问题描述】:

我正在使用带有 TextInputLayout 的 EditText。这是我用来显示错误的代码。

  private boolean validateEmail() {
    String email = inputEmail.getText().toString().trim();

    if (email.isEmpty() || !isValidEmail(email)) {
        inputLayoutEmail.setErrorEnabled(true);
        inputLayoutEmail.setError(getString(R.string.err_msg_email));
        requestFocus(inputEmail);
        return false;
    } else {
        inputLayoutEmail.setErrorEnabled(false);
    }

    return true;
}

我在edittext的textwatcher中调用这个方法,就像这个链接http://www.androidhive.info/2015/09/android-material-design-floating-labels-for-edittext/

一旦我输入了一个有效的输入然后清除它,它将按预期显示错误消息,但如果我再次输入文本然后再次清除它,它将不起作用。即它没有显示任何错误消息。

我正在使用compile 'com.android.support:design:23.1.0' 库。

inputLayoutEmail.setErrorEnabled(true); 

正在调用,但未显示错误。可能是什么问题?我该如何解决这个问题?

【问题讨论】:

  • 更改请求焦点(inputEmail); -> inputEmail.requestFocus();

标签: android validation android-edittext textinputlayout


【解决方案1】:

在您的布局文件中,确保您有 layout_height="wrap_content" 用于 TextInputLayout 而不是维度。这导致了我的问题。

【讨论】:

  • 在所有课程的 4 小时调查后解决了我的问题..谢谢
  • @mg3,很高兴我能帮上忙。我在这方面花费的时间比我想承认的要多,并且没有在其他任何地方看到此解决方案,所以我认为其他人可能会受益!
  • 似乎实际上是更具体的 layout_height - 它不需要设置为 wrap_content 这样 - 只需要足够大 - 但是是的,谢谢我在任何地方都找不到这个! !
  • @philthomas26 从答案和您的评论中,我得到了这个想法,它实际上与身高有关,它现在正在工作,谢谢
  • 你是最棒的!
【解决方案2】:

你只需要申请,

inputLayoutEmail.setErrorEnabled(false);
inputLayoutEmail.setError(null);

它对我有用。希望它也对你有用。

【讨论】:

    【解决方案3】:

    这个例子对我有用。

    你使用

    compile 'com.android.support:design:23.1.0' 
    

    正确的是

    compile 'com.android.support:design:23.0.1' 
    

    【讨论】:

    • 它在第一次工作。第二次以后它没有显示错误。但是得到焦点。
    • 好的。我明白了 - 你使用 compile 'com.android.support:design:23.1.0' 而正确的是 compile 'com.android.support:design:23.0.1'
    • 谢谢..它适用于 com.android.support:design:23.0.1
    • 您能否在您的回答中添加此内容。以便我接受您的回答
    • 如果我的 compile sdk 版本是 24 怎么办?
    【解决方案4】:

    我遇到了同样的问题,我正在使用数据绑定,所以添加以下行解决了我的问题: 应用程序:errorEnabled="true"

    【讨论】:

      【解决方案5】:

      使用 Android 支持库,修订版 23.4.0(2016 年 5 月)

      修复了TextInputLayout 在 API 级别 21 - 22 (Issue 202829)setErrorEnabled(false) 之后无法清除错误色调的问题

      【讨论】:

      • 这个问题似乎在 23.4.0 中没有修复。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      相关资源
      最近更新 更多