首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在EditText下面显示帮助文本以及提示

在EditText下面显示帮助文本以及提示
EN

Stack Overflow用户
提问于 2015-11-24 10:44:13
回答 7查看 29.8K关注 0票数 22

我试着用这些台词来做些什么:

我可以使用android:hint="Email Address"显示提示,但不能显示辅助文本-这将是您的电子邮件用户名。

代码语言:javascript
运行
复制
    <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>`

我得到的只是,没有用户名,最好是您的注册电子邮件,以下编辑:

输出:

任何指南针都会赏识。谢谢。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2015-11-24 10:56:06

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

代码语言:javascript
运行
复制
compile 'com.android.support:design:22.2.0'

然后在xml文件中使用它:

代码语言:javascript
运行
复制
<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>

您可以通过将错误设置为true来设置文本。虽然它是用来显示错误的,但它适合你使用。如果你愿意,你可以改变颜色和字体。

代码语言:javascript
运行
复制
TextInputLayout til = (TextInputLayout)    findViewById(R.id.textInputLayout);
til.setErrorEnabled(true);
til.setError("You need to enter a name");

票数 25
EN

Stack Overflow用户

发布于 2018-11-19 12:59:44

使用Design 28,在TextInputLayout中添加了内置的辅助文本功能。

代码语言:javascript
运行
复制
 implementation 'com.android.support:design:28.0.0'

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

代码语言:javascript
运行
复制
 textInputLayout.isHelperTextEnabled=true
 textInputLayout.error="Email can not be Empty!!!"

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

示例

代码语言:javascript
运行
复制
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!!!"
                }
            }

TextInputLayout / Android开发人员

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

票数 7
EN

Stack Overflow用户

发布于 2021-01-11 10:14:38

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

代码语言:javascript
运行
复制
       <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>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33891482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档