![](/img/trans.png)
[英]Why TextInputEditText cannot be cast to TextInputLayout in Android Studio?
[英]Android Material TextInputLayout and TextInputEditText
我正在尝试检查 Material TextField 是否为空。 如果为空,则应启用错误。 如何在 Material TextField 中设置错误。 如果文本字段不为空,我如何从 Material TextField 中获取文本。
所有这些都需要在 mainactivity.java 中完成
myxml.xml
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_project_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="32dp"
android:hint="Project Name"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp"
app:endIconMode="clear_text"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/projectname_np"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
要检查文本字段是否为空,您可以在 Java 代码中执行此操作 -
if(textInputLayout.getText().toString().matches(""){
// Do something
}
当你想使用它时,你可以在 MainActivity 中使用 textInputLayout.setText() 方法来设置 TextInputLayout 中的文本。 在此处查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.