[英]Android Material TextInputLayout and TextInputEditText
I'm trying to check whether the Material TextField is empty or not.我正在尝试检查 Material TextField 是否为空。 If it is empty, error should be enabled.
如果为空,则应启用错误。 How to set error in Material TextField.
如何在 Material TextField 中设置错误。 And if the text field is not empty how can I fetch the text from the Material TextField.
如果文本字段不为空,我如何从 Material TextField 中获取文本。
All this need's to be done in mainactivity.java所有这些都需要在 mainactivity.java 中完成
myxml.xml 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>
To check if a text field is empty, you can do this in your Java code -要检查文本字段是否为空,您可以在 Java 代码中执行此操作 -
if(textInputLayout.getText().toString().matches(""){
// Do something
}
You can set the text in the TextInputLayout by using textInputLayout.setText() method in your MainActivity when you want to use it.当你想使用它时,你可以在 MainActivity 中使用 textInputLayout.setText() 方法来设置 TextInputLayout 中的文本。 Check out the documentation here.
在此处查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.