【问题标题】:How to fix background red color TextInputLayout when isEmpty in Android如何在Android中的isEmpty时修复背景红色TextInputLayout
【发布时间】:2017-01-04 10:38:34
【问题描述】:

我想要setErrorTextInputLayout isEmpty,我写了这段代码但是当显示错误信息时,为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>

我该如何解决这个问题?谢谢大家

【问题讨论】:

  • 出现错误时,包裹的EditText 的背景会被错误颜色着色。这不是默认材质背景的问题。发布你的drawable/selector_bg_edit。关注这个 Android 问题code.google.com/p/android/issues/…

标签: android android-edittext android-textinputlayout


【解决方案1】:
if (TextUtils.isEmpty(userName)) {
    register_UserName_layout.setError("Insert Username");
    txtInpit.setColorFilter(R.color.white);
}

【讨论】:

  • 你能粘贴你在textinput中设置的属性吗
  • 请看这个:if (TextUtils.isEmpty(userName)) { register_UserName_layout = (TextInputLayout) view.findViewById(R.id.register_userUsernameTextLayout); register_UserName_layout.setError("نام کاربری را وارد نکردید"); register_UserName_layout.setBackgroundColor(getResources().getColor(android.R.color.white)); }
  • 粘贴您的 xml 声明
  • 只需设置颜色过滤器,您的代码就可以工作,我签入我的应用程序然后工作,所以请您更新一下
  • 我使用 compile 'com.android.support:design:24.1.0' 的第 24 版,但没有显示 setColorFilter!为什么?!!!!!!!!!
【解决方案2】:

我找到了解决此问题的方法。您只需要创建一个自定义 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>

【讨论】:

    【解决方案3】:

    类似于 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();
            }
        }
    }
    

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

    【讨论】:

      【解决方案4】:

      您可以继承 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>
      

      【讨论】:

      • 我昨天遇到了同样的问题。这帮助我解决了红色背景问题。谢谢
      【解决方案5】:

      子类 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));
      }
      

      【讨论】:

        【解决方案6】:

        就我而言,我添加了一行

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

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

        【讨论】:

        • 你为我节省了数小时的调查时间:')
        • 这行得通。我的背景颜色为白色,但更改为透明解决了问题
        【解决方案7】:

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

        【讨论】:

        • 这很有效,而且很容易,没有其他更改。我正在使用 TextInputEditText 使用 setBackgroundColor() 设置背景。刚刚使用相同的方法调用更改为 TextInputLayout 并且它起作用了。谢谢!
        • 很高兴我再次遇到这个问题时对此发表了评论......!好的,我现在去学习。
        【解决方案8】:

        首先你的 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>
        

        最重要的是把drawable变成这样的透明颜色 code_view_item.xml

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

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            【解决方案10】:

            我的解决方案:

            yourEdit.background.clearColorFilter()
            

            【讨论】:

              猜你喜欢
              • 2020-03-04
              • 2019-07-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-26
              • 2014-11-15
              相关资源
              最近更新 更多