繁体   English   中英

Android中isEmpty时如何修复背景红色TextInputLayout

[英]How to fix background red color TextInputLayout when isEmpty in Android

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

不想设置背景! 只想显示错误消息。

在此处输入图片说明

我的代码:

if (TextUtils.isEmpty(userName)) {
register_UserName_layout.setError("Insert Username");
}

XML 代码:

<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

我找到了解决此问题的方法。 您只需要创建一个自定义 EditText 并覆盖它的 getBackground() 方法以返回一个新的可绘制对象。 这样 TextInputLayout 将无法在 EditText 的背景上设置颜色过滤器,因为您不返回 EditText 的背景,而是返回另一个可绘制对象。 见下文:

@Override
public Drawable getBackground() {
    return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}

并在 TextInputLayout 中使用自定义 EditText:

<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>

在我的情况下,我添加了行

<solid android:color="@color/transparent"/>

<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>

这只会导致红色边框而不是整个背景

您可以继承 TextInputLayout 并使用它:

package com.mypackage;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;

public class CustomTextInputLayout extends TextInputLayout {
    public CustomTextInputLayout(Context context) {
        super(context);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        clearEditTextColorfilter();
    }
    @Override
    public void setError(@Nullable CharSequence error) {
        super.setError(error);
        clearEditTextColorfilter();
    }

    private void clearEditTextColorfilter() {
        EditText editText = getEditText();
        if (editText != null) {
            Drawable background = editText.getBackground();
            if (background != null) {
                background.clearColorFilter();
            }
        }
    }
}

在您的布局中:

<com.mypackage.CustomTextInputLayout
    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>

这个问题的底线是检查您的 EditText 是否设置了背景颜色。 如果是这样,请将其删除并在文本输入布局小部件上添加背景颜色。 这将解决大红框的问题。 至少对我来说是这样。

子类 EditText 并应用以下方法:

覆盖 getBackground 并创建一个带有输入背景的 Drawable

@Override
public Drawable getBackground() {
    return ContextCompat.getDrawable(getContext(), R.drawable.input_background);
}

你的 Drawable input_brackground 可以是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/inputBackgroundColor">
    </solid>
</shape>

并在 TextInputLayout 设置错误后应用 EditText 子类 setBackground ,例如:

private void showTheError(String error){
    textInputLayout.setError(error);
    editTextSubclassed.setBackgroundColor(getResources().getColor(R.color.inputBackgroundColor));
}

与 Shahin Mursalov 的回答类似,我在构造函数中调用方法init()以在创建视图时存储可绘制的背景。 此外,我还覆盖了setBackgroundResource()方法来存储背景,并从getBackground()返回它:

public class CustomEditText extends EditText{

    private Drawable backgroundDrawable;

    public EditTextContext context) {
        super(context);
        this.context = context;
    }

    public EditTextContext context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }


    public void init(AttributeSet attrs){
        TypedArray attributes = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.background});
        this.backgroundDrawable = attributes.getDrawable(0);
        attributes.recycle();
    }

    @Override
    public void setBackgroundResource(@DrawableRes int resId) {
        this.backgroundDrawable = ContextCompat.getDrawable(context, resId);
        super.setBackgroundResource(resId);
    }

    @Override
    public Drawable getBackground() {
        if (backgroundDrawable != null){
            return backgroundDrawable;
        } else {
            return super.getBackground();
        }
    }
}

通过这种方式,我可以在布局中指定不同的背景并以编程方式更改它。

首先你的 EditText 背景 selector_bg_edit 应该是这样的

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:drawable="@drawable/code_view_item"
        android:state_focused="true"/>

    <item android:drawable="@drawable/sign_in_fields"
        android:state_focused="false"/>

    <item android:drawable="@drawable/sign_in_fields"
        android:state_active="false"/>

</selector>

最重要的是像这样code_view_item.xml一样把drawable放入透明颜色

<stroke android:width="1dp" android:color="#15A859" />
<solid android:color="#00FFFFFF" />
<corners android:radius="12dp"/>

对我有用的是将其放在我的自定义编辑文本类中的以下几行:

override fun getBackground(): Drawable {
    return this.context.getDrawable(R.drawable.myDrawableResource)
}

我的解决方案:

yourEdit.background.clearColorFilter()
if (TextUtils.isEmpty(userName)) {
    register_UserName_layout.setError("Insert Username");
    txtInpit.setColorFilter(R.color.white);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM