2013-01-02 62 views
30

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

目前,它看起来像这样:

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

(它是相同with android:errorMessageBackground

我把这个q这里的问题,因为我已经用尽了想法 - 也许有人已经设法做到这一点?

编辑: 头我使用的主题:

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

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

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

+1

你有没有看过[这个问题](http://stackoverflow.com/questions/6745577/which-theme-attribute-changes-the-text-color-of-an-edittexts-error-message )? – Eric

+0

实际上我做过 - 但我想改变气泡的颜色,而不是文字颜色(我可以改变)。 我注意到克里斯设法改变背​​景 - 我不知道如何。 – scana

+0

似乎'errorMessageBackground'是Android API级别19中引入的新属性。您是否尝试将样式放入'values-v19'文件夹中? –

回答

0
private EditText adTitle; 
// .... 
adTitle.setError(Html.fromHtml("<font color='red'>hello</font>")); 
+0

我想要的是改变EditText的背景,而不是字体颜色本身 - 请在回答之前仔细阅读问题。 – scana

+1

这对我来说真的很有用...... !!!谢谢..!!! –

+16

这不回答问题。 – Garcon

-2

您可以使用此方法只是通过味精文本,你的EditText ID

public static void setErrorMsg(String msg,EditText viewId) 
{ 
    //Osama ibrahim 10/5/2013 
    int ecolor = Color.WHITE; // whatever color you want 
    String estring = msg; 
    ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor); 
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring); 
    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0); 
    viewId.setError(ssbuilder); 

} 
+3

对不起,这不是我想要的 - 我不得不改变整个背景,使它看起来像来自ICS的错误信息。 – scana

5

则需要包含这些依赖关系:

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

这里是一个示例如何使用它:

<android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/input_password" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/hint_email" /> 

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

这会为您提供您正在寻找的材质设计,以便为表单验证以及标签的良好动画效果。

enter image description here

4

我会建议使用@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; 
    } 
} 

绘制/ 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> 

绘制/ 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背景颜色。无论如何,希望这可以帮助某人。

+1

我正在寻找类似这种方法的东西。谢谢 –