将背景设置为Edittext不能正常工作

时间:2016-05-17 11:32:07

标签: android android-edittext android-support-library android-textinputlayout

我在TextInputLayout中使用了edittext,如下面的代码:

<android.support.design.widget.TextInputLayout
            android:id="@+id/tilVoucherNo"
            android:layout_width="225dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/imgVoucher"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            app:errorEnabled="true">

            <EditText
                android:id="@+id/editVoucher"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_edittext_line"
                android:hint="@string/xml_packages_voucher_input_hint"
                android:inputType="textEmailAddress"
                android:paddingBottom="10dp"
                android:paddingRight="25dp" />

        </android.support.design.widget.TextInputLayout>

有了这个依赖:

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'

enter image description here

但是当我想将背景设置为处于错误状态的edittext时,它变得如下图所示: enter image description here

我尝试了两种改变编辑文本背景的解决方案:

editVoucher.getBackground().setColorFilter(getResources().getColor(R.color.edittext_error_inactive_line), PorterDuff.Mode.SRC_IN);

editText.setBackground(getContext().getResources().getDrawable(R.drawable.error_edittext_line));

error_edittext_line是:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:top="-10dp"
    android:left="-10dp"
    android:right="-10dp"
    android:bottom="10dp"
    >
    <shape android:shape="rectangle">
        <stroke android:width="1dp" android:color="@color/edittext_error_active_line"/>
    </shape>
</item>
</layer-list>

我也尝试error_edittext_line以下的形状,但有相同的错误:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-10dp" android:bottom="5dp" android:top="-10dp" android:left="-10dp">
    <shape
        android:shape="rectangle">
        <stroke android:width="1dp" android:color="@color/edittext_error_active_line" />
    </shape>
</item>

0 个答案:

没有答案