首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在安卓isEmpty中修复背景红色TextInputLayout

如何在安卓isEmpty中修复背景红色TextInputLayout
EN

Stack Overflow用户
提问于 2016-08-28 14:33:08
回答 10查看 9.9K关注 0票数 14

当我写这段代码时,我想要setError,但是当显示错误信息时,为TextInputLayout设置红色背景TextInputLayout

I do not want set background!我希望只显示错误信息。

My code:

代码语言:javascript
复制
if (TextUtils.isEmpty(userName)) {
register_UserName_layout.setError("Insert Username");
}

XML代码:

代码语言:javascript
复制
<android.support.design.widget.TextInputLayout
    android:id="@+id/register_userUsernameTextLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/register_headerLayout"
    android:layout_margin="10dp"
    android:textColorHint="#c5c5c5">

    <EditText
        android:id="@+id/register_userUserNameText"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/selector_bg_edit"
        android:hint="نام کاربری"
        android:paddingBottom="2dp"
        android:textColor="@color/colorAccent"
        android:textCursorDrawable="@drawable/bg_input_cursor"
        android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>

我该如何解决这个问题呢?感谢所有<3

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2016-08-28 14:35:17

代码语言:javascript
复制
if (TextUtils.isEmpty(userName)) {
    register_UserName_layout.setError("Insert Username");
    txtInpit.setColorFilter(R.color.white);
}
票数 -4
EN

Stack Overflow用户

发布于 2016-12-13 04:21:24

我已经找到了解决此问题的方法。您只需要创建一个自定义的EditText并覆盖它的getBackground()方法来返回一个新的drawable。这样,TextInputLayout就不能在EditText的背景上设置滤色器,因为你不会返回EditText的背景,而是另一个可绘制的。如下所示:

代码语言:javascript
复制
@Override
public Drawable getBackground() {
    return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}

并在TextInputLayout中使用自定义EditText:

代码语言:javascript
复制
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <CustomEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edit_text_bg" />

</android.support.design.widget.TextInputLayout>
票数 14
EN

Stack Overflow用户

发布于 2018-04-23 22:04:55

在我的例子中,我添加了一行

代码语言:javascript
复制
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:color="@color/lightGray" android:width="1dp"/>
    <solid android:color="@color/transparent"/>
    <padding android:top="7dp" android:bottom="7dp" android:left="7dp" android:right="7dp"/>
    <corners android:radius="2dp"/>
</shape>

这只会产生红色边框,而不是整个背景

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39188609

复制
相关文章

相似问题

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