在EditText下面显示Helper文本以及提示

Ame*_*rke 16 android android-layout android-edittext androiddesignsupport android-textinputlayout

我想在这些方面做点什么:

EditText示例

我能够显示提示android:hint="Email Address"但无法显示帮助文本 - 这将是您的电子邮件用户名

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="15"
            android:hint="Username"
            app:et_helper="Username is preferably your registered email"/>

    </android.support.design.widget.TextInputLayout>`
Run Code Online (Sandbox Code Playgroud)

我得到的只是,并且没有用户名最好是您在edittext下面注册的电子邮件:

输出:

产量

任何指针赞赏.谢谢.

Ami*_*zan 25

最好的方法是使用TextInputLayout.谷歌在新的设计库中引入了它.要使用TextInputLayout,您必须将以下内容添加到build.gradle依赖项中:

compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)

然后在xml文件中使用它:

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Enter your name"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

您可以通过设置错误true来设置文本.尽管它是显示错误,但它适合您的使用.如果需要,您可以更改颜色和字体.

TextInputLayout til = (TextInputLayout)    findViewById(R.id.textInputLayout);
til.setErrorEnabled(true);
til.setError("You need to enter a name");
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @AdarshYadav创建一个自定义样式,在styles.xml文件中使用`@android:style/TextAppearance`作为父项,并在TextInputLayout小部件`app中使用它:errorTextAppearance` (3认同)

Tec*_*hAJ 6

使用设计支持库28,在TextInputLayout中添加了内置的帮助器Text功能。

 implementation 'com.android.support:design:28.0.0'
Run Code Online (Sandbox Code Playgroud)

现在使用xml或以编程方式启用错误

 textInputLayout.isHelperTextEnabled=true
 textInputLayout.error="Email can not be Empty!!!"
Run Code Online (Sandbox Code Playgroud)

另外,提示和错误现在可以一起使用!!!

et.setOnFocusChangeListener { v, b ->
            if (b) {
                textInputLayout.helperText = "yourhelperText"

            } else {
                textInputLayout.helperText = null
                if(et.text.toString()==""){    // or any other validation
                    textInputLayout.error="Email can not be Empty!!!"
                }
            }
Run Code Online (Sandbox Code Playgroud)

TextInputLayout | Android开发人员

编辑不要忘记通过xml或以编程方式启用错误和helperText。


Emm*_*ngo 5

具有 TextInputLayout OutlinedBox 样式和 TextInputEditText 的完整 XML

       <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til_customer_no"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:padding="3dp"
            app:helperTextEnabled="true"
            app:helperText="* Enter customer number that will be used">

            <!--android:maxLength="13"-->
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et_customer_no"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Customer No"
                android:inputType="number" />

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

Run Code Online (Sandbox Code Playgroud)