2017-06-03 9 views
0

私は完全なUIのカスタマイズを行っているアプリを開発中です。 EditTextTextInputLayoutにラップされている場合、EditTextのデフォルトのテキストヒントカラーとラインカラーは、スタイルで定義されているtextPrimaryColorと同じです。ただし、EditTextがフォーカスを受け取ると、その行はスタイルで定義されたaccentの色になり、ヒントは浮動ヒントアニメーションを通り、accentの色に変わります。 styles.xmlthemes.xmlとするのはかなり簡単ですが、私はそれをプログラム的に得ることはできません。Android TextInputLayoutスタイリング(プログラム)

私が設定している現在の方法はこれです:

public static void setInputTextLayoutColor(final int accent, final int text, TextInputLayout textInputLayout, AppCompatEditText edit) { 
    edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      AppCompatEditText editText = (AppCompatEditText) v; 
      editText.getBackground().clearColorFilter(); 
      if(hasFocus) editText.getBackground().setColorFilter(accent, PorterDuff.Mode.SRC_IN); 
      else editText.getBackground().setColorFilter(text, PorterDuff.Mode.SRC_IN); 
     } 
    }); 


    setCursorColor(edit, accent); 

    try { 
     Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor"); 
     field.setAccessible(true); 
     int[][] states = new int[][]{ 
       new int[]{} 
     }; 
     int[] colors = new int[]{ 
       accent 
     }; 
     ColorStateList myList = new ColorStateList(states, colors); 
     field.set(textInputLayout, myList); 

     Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor"); 
     fDefaultTextColor.setAccessible(true); 
     fDefaultTextColor.set(textInputLayout, myList); 

     Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class); 
     method.setAccessible(true); 
     method.invoke(textInputLayout, true); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

この方法の問題は、ヒントテキストがアクセントカラーにすぐに着色されているA)、それがアクセントときに着色していないということですフォーカスを受け取ったB)EditText行は正しい色で始まりますが、フォーカスを受け取ると、プログラムで設定された色ではなく、スタイルで定義されたaccentに色付けされます。

これは焦点を当てていない画像です(「イベント名」フィールドを参照)。ここで、「イベント名」の色はグレー/ホワイト(テキストのように)である必要があります。 Non-focused <code>TextInputLayout</code>

これはフォーカスされた画像です。ここではすべてが線の色以外は緑です。うまくFocused <code>TextInputLayout</code>

は、任意の助けいただければ幸いです

答えて

0

が動的にヒント色の変更を把握することができませんでしたが、私はリスナーに変更する線の色を得た:。

AppCompatEditText edit = (AppCompatEditText)findViewById(R.id.event_create_name_edit); 
    edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      AppCompatEditText edit2 = (AppCompatEditText)v; 
      if(hasFocus) edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getAccent())); 
      else edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getText())); 
     } 
    }); 
関連する問題