简体   繁体   English

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

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

I want setError when TextInputLayout isEmpty , I write this code but when show error message, set red background for TextInputLayout !TextInputLayout isEmpty时我想要setError ,我写了这段代码但是当显示错误消息时,为TextInputLayout设置红色背景!

I do not want set background!不想设置背景! I want just show the error message.只想显示错误消息。

在此处输入图片说明

My code:我的代码:

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

XML code : 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>

How can I fix this?我该如何解决这个问题? Thanks all <3谢谢大家 <3

I have found a workaround to this problem.我找到了解决此问题的方法。 You just need to create a custom EditText and override the getBackground() method of it to return a new drawable.您只需要创建一个自定义 EditText 并覆盖它的 getBackground() 方法以返回一个新的可绘制对象。 That way TextInputLayout won't be able to set color filter on the EditText's background, since you do not return EditText's background, but some another drawable.这样 TextInputLayout 将无法在 EditText 的背景上设置颜色过滤器,因为您不返回 EditText 的背景,而是返回另一个可绘制对象。 See below:见下文:

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

and use the custom EditText inside TextInputLayout:并在 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>

in my case I added line在我的情况下,我添加了行

<solid android:color="@color/transparent"/> <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>

this results in red border only not entire background这只会导致红色边框而不是整个背景

You can subclass TextInputLayout and use that:您可以继承 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();
            }
        }
    }
}

in your layout:在您的布局中:

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

Bottom line with this issue is check if your EditText has a background color set.这个问题的底线是检查您的 EditText 是否设置了背景颜色。 If so, remove it and put a background color on your text Input Layout widget instead.如果是这样,请将其删除并在文本输入布局小部件上添加背景颜色。 This will fix the issue of the big red box.这将解决大红框的问题。 At least it did for me.至少对我来说是这样。

Subclass EditText and apply the follow methods:子类 EditText 并应用以下方法:

Override getBackground and create a Drawable with input background覆盖 getBackground 并创建一个带有输入背景的 Drawable

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

your Drawable input_brackground can be like:你的 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>

And apply EditText subclassed setBackground after TextInputLayout set error like:并在 TextInputLayout 设置错误后应用 EditText 子类 setBackground ,例如:

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

Similar to Shahin Mursalov's answer, I call method init() in the costructor to store the background drawable when the view is created.与 Shahin Mursalov 的回答类似,我在构造函数中调用方法init()以在创建视图时存储可绘制的背景。 Also I've overriden method setBackgroundResource() to store the background as well, and return it from getBackground() :此外,我还覆盖了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();
        }
    }
}

This way I can specify a different background in my layout and change it programatically.通过这种方式,我可以在布局中指定不同的背景并以编程方式更改它。

first your EditText background selector_bg_edit should be like that首先你的 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>

And the most important thing is to put in the drawable to be transparent color like this code_view_item.xml最重要的是像这样code_view_item.xml一样把drawable放入透明颜色

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

What worked for me is just putting this below lines in my custom edit text class :对我有用的是将其放在我的自定义编辑文本类中的以下几行:

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