3

有沒有一種方法來定製這些TextInputLayout性能編程:風格TextInputLayout編程

  • textColorHint
  • colorAccent
  • colorControlNormal
  • colorControlActivated
  • textSelectHandle

我知道如何使用主題屬性來設置它們,但是我正在處理的項目會動態加載顏色信息,並且據我所知,在運行時無法更改主題/樣式值。

+0

您可以在此找到答案:http://stackoverflow.com/questions/31722034/how-對變化顏色的-textinputlayouts標籤和-的EditText下劃線-A ndroid/31723120#31723120 – guisantogui

+1

問題是以編程方式設置它。 –

回答

0

我寫的textInputLayout實現,它可以設置任何顏色的下劃線和錯誤,並顯示錯誤的頂部。

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); 
    } 
} 
} 

風格

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