简体   繁体   English

Android 材质 TextInputLayout 和 TextInputEditText

[英]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.

相关问题 为什么 TextInputEditText 在 Android Studio 中无法转换为 TextInputLayout? - Why TextInputEditText cannot be cast to TextInputLayout in Android Studio? Android 应用程序中的“TextInputEditText 无法转换为 TextInputLayout” - “TextInputEditText cannot be cast to TextInputLayout” in Android app Android TextInputEditText/TextInputLayout 切断错误信息 - Android TextInputEditText/TextInputLayout cuts off error message 如何在 Android Material TextInputLayout 中将 SpannableString 设置为 EditText? - How to set a SpannableString to an EditText in an Android Material TextInputLayout? 如何从 TextInputLayout 或 TextInputEditText 中删除下划线 - How can remove underline from TextInputLayout or TextInputEditText Android Material TextInputLayout endIcon 截断 MaterialAutoCompleteTextView 中的文字 - Android Material TextInputLayout endIcon cuts off text in MaterialAutoCompleteTextView 如何去除Android Material Design中TextInputLayout下的下划线? - How to remove the underline under TextInputLayout in Android Material Design? Android 不保存 TextInputEditText 的状态 - Android not saving state of TextInputEditText TextInputLayout:添加的EditText不是TextInputEditText。 请切换到使用该类 - TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead 二进制 XML 文件第 41 行:膨胀 class com.google.ZC31B32364CE19CA8FCD150A. - Binary XML file line #41: Error inflating class com.google.android.material.textfield.TextInputLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM