How to set TextInputLayout error message colour?

前端 未结 9 2205
旧巷少年郎
旧巷少年郎 2020-11-27 13:58

How can I change the colour of the error message that can be set to appear below the text field in a TextInputLayout (via setError(...) – see error

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 14:32

    I needed to do this dynamically. Using reflection:

    public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
      try {
        Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
        fErrorView.setAccessible(true);
        TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
        Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
        fCurTextColor.setAccessible(true);
        fCurTextColor.set(mErrorView, color);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    

    You will need to call textInputLayout.setErrorEnabled(true) before invoking the above method for this to work.

提交回复
热议问题