android textInputEditText with custom background is not working ok

旧时模样 提交于 2019-12-10 23:03:13

问题


i'm using textInputEditText inside textInputLayout i had to set background for my editText to achieve a bordered view for my editText. but when i call setError() on my textInputLayout the entire editText color changes to red. but i want to change only the color of error text, not the entire view.

before setting error :

screen shot

after setting error :

screen shot

and here is my xml code :

<android.support.design.widget.TextInputLayout
            android:layout_alignParentTop="true"
            android:id="@+id/ex_pass_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="false"
            android:gravity="right">
            <android.support.design.widget.TextInputEditText
                android:id="@+id/ex_pass_et"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:hint="رمز عبور فعلی"
                android:inputType="textPassword"
                android:textColor="#000"
                android:textSize="15sp"
                android:gravity="right|center"
                android:background="@drawable/edittext_bg"
                android:padding="8dp"
                />
        </android.support.design.widget.TextInputLayout>

please help me, what am i doing wrong?


回答1:


i fixed this by extending TextInputLayout and overriding some methods

public class CustomTextInputLayout extends TextInputLayout {

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



    @Override
    public void setError(@Nullable CharSequence error) {
        super.setError(error);
        try {
            EditText et = getEditText();
            Drawable editTextBackground = et.getBackground();
            editTextBackground.clearColorFilter();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        try {
            EditText et = getEditText();
            Drawable editTextBackground = et.getBackground();
            editTextBackground.clearColorFilter();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


来源:https://stackoverflow.com/questions/44741574/android-textinputedittext-with-custom-background-is-not-working-ok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!