首页 > 解决方案 > Android Material design 错误验证不出现

问题描述

当我向编辑文本添加错误消息时。

我看到了图标,但默认情况下单击图标后不会出现错误消息

这是我的布局文本

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

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_centerVertical="true"
                android:hint="@string/enter_client_name">


                <com.google.android.material.textfield.TextInputEditText
                    app:errorEnabled="true"
                    android:id="@+id/editTextClientName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </com.google.android.material.textfield.TextInputLayout>

这是java代码:

if(t.getClientName().equals("")){
            editTextClientName.setError( "Client name is required!" );
            return false;
        }

这是结果

标签: androidmaterial-designmaterial-components-android

解决方案


您只是犯了一个简单的错误,给您的 TextInputLayout 一个 id,然后在其上调用 setError 方法而不是 TextInputEditText。

做这个 :

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

            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_centerVertical="true"
            android:hint="@string/enter_client_name">


            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editTextClientName"
                app:errorEnabled="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </com.google.android.material.textfield.TextInputLayout>

Java代码:

if(t.getClientName().equals("")){
        textInputLayout.setError( "Client name is required!" );
        return false;
    }

推荐阅读