设计Android EditText以显示谷歌描述的错误消息

kmn*_*kmn 140 android android-edittext

我需要一个EditText看起来像这样的onError:

在此输入图像描述

调用onError看起来像这样:

在此输入图像描述

注意:该应用程序在SDK 19(4.4.2)上运行

min SDK是1

有没有类似于setError的方法自动执行此操作,还是我必须为其编写代码?

谢谢

reV*_*rse 310

有没有需要使用第三方库,因为谷歌推出TextInputLayout的为部分design-support-library.

遵循一个基本的例子:

布局

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

注意:通过设置app:errorEnabled="true"它的属性,TextInputLayout一旦显示错误就不会改变它的大小 - 所以它基本上阻止了空间.

为了显示下面的错误的EditText,你只需要调用#setErrorTextInputLayout(不上孩子EditText):

TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");
Run Code Online (Sandbox Code Playgroud)

结果

图片显示带有错误消息的编辑文本

要隐藏错误并重置色调,只需调用即可til.setError(null).


注意

为了使用TextInputLayout您必须将以下内容添加到您的build.gradle依赖项:

dependencies {
    compile 'com.android.support:design:25.1.0'
}
Run Code Online (Sandbox Code Playgroud)

设置自定义颜色

默认情况下,该行的行为EditText红色.如果需要显示不同的颜色,可以在调用后立即使用以下代码setError.

editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

要清除它,只需调用该clearColorFilter函数,如下所示:

editText.getBackground().clearColorFilter();
Run Code Online (Sandbox Code Playgroud)

  • 只是想注意,因为它是如此小的细节 - 我通过在`EditText`上调用setError而不是`TextInputLayout`来解决这个问题.我看到了这个答案,仍然无法弄清楚我需要改变什么.很容易错过. (13认同)
  • @Alessio我一直试图说,当你扩展AppCompatActivity它应该有效.然而:从appcompat-v23开始,没有必要再设置colorFilter,只要你调用`textInputLayout.setError("Error messsage")```EditText`的颜色应该变成红色.为了重置它,只需调用`textInputLayout.setError(null)`即可. (5认同)
  • 是的!而已 !你怎么让线条变红? (3认同)
  • @kmn使线条变红并不是开箱即用的,所以你需要自己做.看我的编辑.在移动提示方面:这是不可能的. (3认同)
  • `editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary),PorterDuff.Mode.SRC_ATOP);`不再需要`使用最新的支持库 (3认同)

aqe*_*qel 7

EditText应该被包裹起来TextInputLayout

<android.support.design.widget.TextInputLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tilEmail">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/etEmail"
        android:hint="Email"
        android:layout_marginTop="10dp"
        />

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

要获得您想要的错误消息,请将错误设置为 TextInputLayout

TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
    tilEmail.setError("Invalid email id");    
}
Run Code Online (Sandbox Code Playgroud)

您应该添加设计支持库依赖项.在gradle依赖项中添加此行

compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)


Zon*_*Zon 6

呼叫myTextInputLayout.setError()而不是myEditText.setError()

这些容器和容器在设置错误方面具有双重功能。您需要的功能是容器的功能。但是您可能需要最低版本的23。


eri*_*icn 5

reVerse 的答案很好,但它没有指出如何删除浮动错误工具提示之类的东西

你需要edittext.setError(null)删除它。
另外,正如有人指出的那样,你不需要TextInputLayout.setErrorEnabled(true)

布局

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter something" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

代码

TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);
Run Code Online (Sandbox Code Playgroud)