android - 更改 EditText 错误信息的背景

标签 android input popup android-edittext drawable

我要做的是更改使用 setError() 方法后显示的弹出错误消息的背景颜色(设置自定义可绘制对象)。

目前,它看起来像这样:

enter image description here

我发现Android有两个文件:

  • popup_inline_error.9.png
  • popup_inline_above_error.9.png

你应该能够使用两个属性来设置它们:

  • errorMessageBackground
  • errorMessageAboveBackground

但是当我尝试在我的主题中设置它们时,我得到的只是:

<item name="errorMessageBackground">@drawable/popup_inline_error_holo_light</item>
<item name="errorMessageAboveBackground">@drawable/popup_inline_error_above_holo_light</item>

error: Error: No resource found that matches the given name: attr 'errorMessageBackground'.

(和 android:errorMessageBackground 是一样的)

我把这个问题放在这里,因为我已经没有想法了——也许有人已经设法做到了?

编辑: 我正在使用的主题的标题:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style
        name="Theme.MyThemeName"
        parent="@style/Theme.Sherlock.Light">

另一个编辑: 呃,我发现我的问题是重复的: android:errorMessageBackground getting no resource found error in styles.xml

还有一个编辑: 这是一个已知问题,请查看此链接:https://code.google.com/p/android/issues/detail?id=55879

最佳答案

我建议使用 @Codeversed solution ,但如果由于某种原因它不适合您,您可以使用我的自定义 EditText 实现。

通常的 EditText 表示: enter image description here

EditText 错误: enter image description here

简而言之:我为错误显示创建了自定义 xml 状态。相关代码见下方:

InputEditText.java:

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import com.example.oleksandr.inputedittext.R;

/**
 * Input EditText which allows define custom drawable for error state
 */
public class InputEditText extends EditText {

    private static final int[] STATE_ERROR = {R.attr.state_error};

    private boolean mIsError = false;

    public InputEditText(Context context) {
        this(context, null, 0);
        init();
    }

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

    public InputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public InputEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // empty
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {
                // empty
            }
        });
    }

    @Override
    public void setError(CharSequence error) {
        mIsError = error != null;
        super.setError(error);
        refreshDrawableState();
    }

    @Override
    public void setError(CharSequence error, Drawable icon) {
        mIsError = error != null;
        super.setError(error, icon);
        refreshDrawableState();
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (mIsError) {
            mergeDrawableStates(drawableState, STATE_ERROR);
        }
        return drawableState;
    }
}

drawable/edittext_bg_error.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    android:id="@+id/listview_background_shape"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <stroke
        android:width="2dp"
        android:color="#f00"
        />
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp"
        />
    <corners android:radius="5dp"/>
    <solid android:color="#ffffffff"/>
</shape>

drawable/edittext_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- custom error state drawable -->
    <item android:drawable="@drawable/edittext_bg_error" app:state_error="true"/>

    <!-- Do whatever you want for all other states -->
    <item android:drawable="@android:drawable/editbox_background_normal"/>
</selector>

添加到您的 attrs.xml

<attr name="errorColor" format="reference"/>

styleables.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="error">
        <attr name="state_error" format="boolean"/>
    </declare-styleable>
</resources>

而且用法很简单:

<com.example.oleksandr.inputedittext.views.InputEditText
    android:id="@id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_bg_selector"
    android:inputType="text"
    android:text="@string/hello_world"
    />

[编辑]:

刚刚意识到,原来的答案是关于改变错误弹出颜色,而不是 EditText 背景颜色。无论如何,希望这可以帮助某人。

关于android - 更改 EditText 错误信息的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127710/

相关文章:

java - Thread.stop() 的后果

android - RxJava - 在 "subscribe"lambda 中使用不同类型的变量(多个链运算符的结果)

input - 如何在 .bat 中请求和接收用户输入并使用它来运行某个程序?

gwt - 我们如何计算处理时间?

swift - 按 T​​abBar 项目后弹出

android - 使用 Glide 库时出现异常

java - Android - 如何将接口(interface)传递给AsyncTask

javascript - 我需要替换此输入字段中的占位符

使用按键进行 Javascript 输入验证,获取前后值

javascript - 使用 JavaScript 检查地址栏是否显示(适用于 Internet Explorer)