3

Gibt es eine Möglichkeit diese TextInputLayout Eigenschaften programmatisch anpassen:Stil TextInputLayout programmatisch

  • textColorHint
  • colorAccent
  • colorControlNormal
  • colorControlActivated
  • textSelectHandle

Ich weiß, wie man sie mit Themenattributen stylt, aber das Projekt, an dem ich gerade arbeite, lädt Farbinformationen dynamisch, und soweit ich weiß, gibt es keine Möglichkeit, zur Laufzeit Themen-/Stilwerte zu ändern.

+0

Sie hier die Antwort finden: http://stackoverflow.com/questions/31722034/how- Textfarbe ändern -eingebenlayouts-label-und-edittext-unterstreichen-a ndroid/31723120 # 31723120 – guisantogui

+1

Die Frage war, es programmatisch zu setzen. –

Antwort

0

Ich schreibe textInputLayout-Implementierung, die jede Farbe der Unterstreichung und Fehler und Fehler oben zeigen kann.

enter image description here

enter image description here

enter image description here

public class TextInputLayoutUseful extends TextInputLayout { 
private CharSequence originalHint = ""; 

public TextInputLayoutUseful(Context context) { 
    super(context); 
    init(); 
} 

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

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

private void init() { 
    this.originalHint = getHint(); 
    setErrorEnabled(false); 
} 

/** 
* - put error text into top floating label 
* - colorized layout 
* 
* @param error error text 
*/ 
@Override 
public void setError(@Nullable CharSequence error) { 
    if (error == null) { 
     setHint(originalHint); 
     setHintTextAppearance(R.style.InputLayoutNormalHint); 
     setUnderlineColor(R.color.input_normal_accent); 
    } else { 
     setHint(error); 
     setHintTextAppearance(R.style.InputLayoutErrorHint); 
     setUnderlineColor(R.color.input_error_accent); 
    } 
} 

/** 
* colorized layout specified green color 
* 
* @param acceptedHint text for floating label 
*/ 
public void setGreenError(@NonNull CharSequence acceptedHint) { 
    setHint(acceptedHint); 
    setHintTextAppearance(R.style.InputLayoutAcceptedHint); 
    setUnderlineColor(R.color.input_accepted_accent); 
} 

private void setUnderlineColor(@ColorRes int colorRes) { 
    if (getEditText() != null) { 
     getEditText().getBackground().mutate().setColorFilter(ContextCompat.getColor(getContext(), colorRes), PorterDuff.Mode.SRC_ATOP); 
    } 
} 
} 

Stil

<style name="InputLayoutErrorHint" parent="TextAppearance.AppCompat"> 
    <item name="android:textColor">@color/input_error_accent</item> 
</style> 

<style name="InputLayoutNormalHint" parent="TextAppearance.AppCompat"> 
    <item name="android:textColor">@color/input_normal_accent</item> 
</style> 

<style name="InputLayoutAcceptedHint" parent="TextAppearance.AppCompat"> 
    <item name="android:textColor">@color/input_accepted_accent</item> 
</style>