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

时间:2015-06-20 11:28:41

标签: android android-edittext

我需要EditText看起来像这样的错误:

enter image description here

调用onError会改为:

enter image description here

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

min SDK是1

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

谢谢

7 个答案:

答案 0 :(得分:287)

由于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的属性,一旦显示错误,它就不会改变它的大小 - 所以它基本上阻止了空间。

代码

为了在EditText下方显示错误,您只需在#setError(不在孩子TextInputLayout上)致电EditText

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

结果

picture showing the edit text with the error message

要隐藏错误并重置色调,只需致电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();

答案 1 :(得分:6)

您的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依赖项中添加此行

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

答案 2 :(得分:4)

致电myTextInputLayout.setError()而非myEditText.setError()

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

答案 3 :(得分:3)

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

答案 4 :(得分:1)

TextInputLayout til = (TextInputLayout)editText.getParent();
til.setErrorEnabled(true);
til.setError("some error..");

答案 5 :(得分:0)

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

您可能不是在TextTextLayout上调用 setError ,而是在EditText上使用了 setError

答案 6 :(得分:0)

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