小编Har*_*rdy的帖子

将错误设置为Android M上的TextInputLayout后更改EditText背景的问题

我已将自定义背景设置为Xml文件中的EditText.验证后,我在运行时为EditText设置了不同的背景,并将错误设置为TextInputLayout.但是在Android M中,它不是设置后台资源,而是将背景颜色设置为EditText.

这是在将错误设置为TextInputLayout之前的图像

这是将错误设置为TextInputLayout后的图像

以下是我的代码: -

XML文件代码

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:errorTextAppearance="@style/ErrorText">

<EditText
    android:id="@+id/input_name"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@drawable/edittext_selector"
    android:hint="@string/hint_name"
    android:imeOptions="actionNext"
    android:inputType="textEmailAddress" />

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

验证码

if (inputName.getText().toString().trim().isEmpty()) {
    inputLayoutName.setErrorEnabled(true);
    inputLayoutName.setError("Errorrroror");
    inputName.setBackgroundResource(R.drawable.edittext_red_focused);
    requestFocus(inputName);
    return false;
} else {
    inputName.setBackgroundResource(R.drawable.edittext_selector);
    inputLayoutName.setErrorEnabled(false);
}

return true;
Run Code Online (Sandbox Code Playgroud)

以下是我的错误资源文件

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

<!-- main color -->
<item
android:bottom="1.0dp"
android:left="1.0dp"
android:right="1.0dp">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>

<!-- draw another block to cut-off the left and right bars --> …
Run Code Online (Sandbox Code Playgroud)

android android-edittext android-textinputlayout

4
推荐指数
1
解决办法
2850
查看次数