2013-01-02 63 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

我正在尋找類似這種方法的東西。謝謝 –