设计 Android EditText 以显示 google 所述的错误消息

Design Android EditText to show error message as described by google

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

调用 onError 看起来像这样:

注意:该应用在 SDK 19 (4.4.2)

上 运行

最小 SDK 为 1

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

谢谢

无需使用第三方库,因为 Google 将 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>

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

代码

为了在 EditText 下方显示错误,您只需在 TextInputLayout 上调用 #setError(而不是在子 EditText 上):

TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");

结果

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


备注

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

dependencies {
    compile 'com.android.support:design:25.1.0'
}

设置自定义颜色

默认情况下,EditText 行将为红色。如果您需要显示不同的颜色,您可以在调用 setError.

后立即使用以下代码
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);

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

editText.getBackground().clearColorFilter();

你的 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>

要获得所需的错误消息,请将错误设置为 TextInputLayout

TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
    tilEmail.setError("Invalid email id");    
}

您应该添加设计支持库依赖项。在你的 gradle dependencies

中添加这一行
compile 'com.android.support:design:22.2.0'
TextInputLayout til = (TextInputLayout)editText.getParent();
til.setErrorEnabled(true);
til.setError("some error..");

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>

代码

TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);

调用 myTextInputLayout.setError() 而不是 myEditText.setError()

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

如果有人在使用 google 的设计库时仍然遇到答案中提到的错误,请使用 @h_k 评论的 -

您可以在 EditText 本身上使用 setError,而不是在 TextInputLayout 上调用 setError。

private EditText edt_firstName;
private String firstName;

edt_firstName = findViewById(R.id.edt_firstName);

private void validateData() {
 firstName = edt_firstName.getText().toString().trim();
 if (!firstName.isEmpty(){
//here api call for ....
}else{
if (firstName.isEmpty()) {
                edt_firstName.setError("Please Enter First Name");
                edt_firstName.requestFocus();
            } 
}
}