android - 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;
}
解决方案
您只是犯了一个简单的错误,给您的 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;
}
推荐阅读
- sql - SQL 中的 PIVOT 给出错误 - ORA-00933: SQL 命令未正确结束
- python - 如何从位于不同目录中的另一个 python 文件执行 python 文件
- python - 用beautifulsoup从天才歌词中获取歌曲歌词│python 3.8
- azure - 将不同结构的文档存储在同一个 Azure COSMOS DB 容器中
- amazon-web-services - 什么是增强型 Vpc 路由的 Redshift 参数
- cmake - CMake:调用自定义命令作为 make install 的一部分
- ringcentral - 在 RingCentral 检查语音邮件不同的分机
- c# - 将列表框绑定到数据源(SQL Server 数据库)后为空:C#、Windows 窗体
- javascript - 如何实现全响应?
- cassandra - 埃拉桑德拉和卡桑德拉