2016-08-25 59 views
2

我希望TextInputLayouts的浮動標籤在出現錯誤時更改顏色(例如,變爲紅色)。我可以更改錯誤文本的顏色,但它對浮動標籤的外觀沒有影響(不像其他某個其他線索聲稱的那樣)。我無法使用提示顏色的選擇器來解決這個問題,因爲似乎沒有爲錯誤定義的狀態。有沒有人有任何想法如何做到這一點,而不必手動編程錯誤事件的變化/創建一個新的Java類(與EditText作爲父)?Android:在錯誤狀態下更改TextInputLayout中浮動標籤(提示)的顏色

這裏是我所定義的兩種風格:

<style name="EditTextFloatingLabel" parent="@android:style/TextAppearance"> 
    <item name="android:textSize">@dimen/textsize_caption_small</item> 
    <item name="android:layout_marginBottom">8dp</item> 
    <item name="android:textColor">@color/input_text_color</item> 
</style> 

<style name="EditTextErrorText" parent="@android:style/TextAppearance"> 
    <item name="android:textColor">@color/error_color</item> 
</style> 

<style name="EditTextLayout"> 
    <item name="android:textColorHint">@color/placeholder_color</item> 
    <item name="android:background">@drawable/input_field_background</item> 
    <item name="android:paddingBottom">@dimen/default_margin_bottom</item> 
    <item name="android:paddingStart">@dimen/default_margin_left</item> 
    <item name="android:paddingEnd">@dimen/default_margin_right</item> 
</style> 

<style name="EditTextTheme"> 
    <item name="android:imeOptions">actionDone</item> 
    <item name="android:maxLines">1</item> 
    <item name="colorControlNormal">@color/primary_line_color</item> 
    <item name="colorControlActivated">@color/nordea_blue</item> 
    <item name="android:colorControlHighlight">@color/error_color</item> 
    <item name="android:textColorPrimary">@color/input_field_text</item> 
    <item name="android:textSize">@dimen/textsize_caption</item> 
    <item name="android:textColorHint">@color/placeholder_color</item> 

</style> 

<style name="EditText"> 
    <item name="android:theme">@style/EditTextTheme</item> 
    <item name="android:textCursorDrawable">@drawable/cursor_blue</item> 
    <item name="android:paddingTop">@dimen/default_padding_top</item> 
    <item name="android:paddingStart">@dimen/payment_text_input_padding</item> 
</style> 

用法:

 <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_field_error_wrapper_light" 
      style="@style/EditTextLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="@dimen/testlogin_text_input_end_padding" 
      android:layout_marginStart="@dimen/testlogin_text_input_start_padding" 
      android:paddingTop="@dimen/default_padding_top" 
      app:hintTextAppearance="@style/EditTextFloatingLabel" 
      app:errorTextAppearance="@style/EditTextErrorText" 
      app:errorEnabled="true"> 

      <EditText 
       android:id="@+id/input_field_light_error" 
       style="@style/EditText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="#Input Field Disabled Light" 
       android:imeOptions="actionDone" 
       /> 
     </android.support.design.widget.TextInputLayout> 

這是我所看到的:

enter image description here

+0

嘗試我的更新回答 –

回答

3

因爲我需要執行一些其他操作,以及當佈局是在錯誤狀態下,我決定採用創建自定義錯誤狀態的解決方案和可以進入此狀態的自定義TextInputLayout。

代碼:

定義在res /價值/ attrs.xml新的錯誤狀態:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    ... 

    <!-- custom states --> 
    <declare-styleable name="ErrorState"> 
     <attr name="state_error" format="boolean" /> 
    </declare-styleable> 
</resources> 

ErrorStateTextInputLayout類:

public class ErrorStateTextInputLayout extends TextInputLayout { 
    private boolean hasError = false; 
    private static final int[] ERROR_STATE = new int[]{R.attr.state_error}; 

    public ErrorStateTextInputLayout(Context context) { 
     super(context); 
    } 

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

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

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
     int[] state = super.onCreateDrawableState(extraSpace + 1); 
     if (hasError) { 
      mergeDrawableStates(state, ERROR_STATE); 
     } 
     return state; 
    } 

    @Override 
    public void setError(@Nullable CharSequence error) { 
     if (error == null) { 
      setHasError(false); 
     } else { 
      setHasError(true); 
     } 
     super.setError(error); 
    } 


    public void setHasError(boolean error) { 
     this.hasError = error; 
     refreshDrawableState(); 
    } 
} 

選擇設置的提示文本顏色:

<?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"> 
    <item android:color="@color/disabled_text_color" android:state_enabled="false" /> 
    <item android:color="@color/error_color" app:state_error="true" /> 
    <item android:color="@color/input_text_color" android:state_focused="true" /> 
    <item android:color="@color/placeholder_color" /> 
</selector> 

這個博客文章幫了我很多:http://code.neenbedankt.com/example-of-custom-states-in-android-components/

+0

你是怎麼做到的?調用setHasError(true)不適合我。我已將選擇器設置爲textColor for error_appearance。 – NightFury

+0

@NightFury我對錯誤文本使用單一顏色,所以我不確定選擇器(我認爲它應該也適用於此)。對我來說,只需定義一個自定義狀態屬性並在適當的位置設置它的狀態就可以實現,就像在上面的代碼片段中看到的一樣。也許如果你也發佈了一些代碼部分,我可以更好地理解你的問題可能在哪裏。 – hildegard

0

嘗試this..solid_red浮色.....

<style name="TextLabelInput" parent="TextAppearance.AppCompat"> 
<!-- Hint color and label color in FALSE state --> 
<item name="android:textColorHint">@color/solid_red</item> 
<item name="android:textSize">16sp</item> 
<item name="android:textStyle">bold</item> 
<item name="android:textColor">@color/solid_red</item> 
<item name="android:duration">200</item> 
<item name="android:textColorHighlight">@color/solid_red</item> 

<!-- Label color in TRUE state and bar color FALSE and TRUE State --> 
<item name="colorAccent">@color/solid_red</item> 
<item name="colorControlNormal">@color/solid_red</item> 
<item name="colorControlActivated">@color/solid_red</item> 
<item name="colorPrimary">@color/solid_red</item> 
<item name="colorPrimaryDark">@color/solid_red</item> 

<android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     app:errorTextAppearance="@style/TextLabelInput" 

     > 

提示文本顏色管理在你的主旋律

這是colorPrimary和colorAccent

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 

</style> 
+0

這不適合我。下劃線和錯誤消息是紅色的,但浮動文本仍然是重音顏色。 – Laranjeiro

相關問題