2017-12-28 20 views
0

編集テキストの色をプログラムで変更しようとしています。それは動作しますが、あなたが添付された画像から見ることができるように、テキスト選択のアイコンは、まだ私が設定した青の代わりにテーマの色のアクセントを使用しています。どうすれば変更できますか?私の現在のコードは次のとおりです。色の編集テキスト選択ハンドルをプログラムで変更する

editText.setBackgroundTintList(new ColorStateList(new int[][]{StateSet.WILD_CARD}, new int[]{color})); 
setCursorDrawableColor(editText, color); 

private void setCursorDrawableColor(EditText editText, int color) { 
    try { 
     Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); 
     fCursorDrawableRes.setAccessible(true); 
     int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); 
     Field fEditor = TextView.class.getDeclaredField("mEditor"); 
     fEditor.setAccessible(true); 
     Object editor = fEditor.get(editText); 
     Class<?> clazz = editor.getClass(); 
     Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); 
     fCursorDrawable.setAccessible(true); 

     Drawable[] drawables = new Drawable[2]; 
     Resources res = editText.getContext().getResources(); 
     drawables[0] = res.getDrawable(mCursorDrawableRes); 
     drawables[1] = res.getDrawable(mCursorDrawableRes); 
     drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     fCursorDrawable.set(editor, drawables); 
    } catch (final Throwable ignored) { 
    } 
} 

edit text with color accent

+0

はなかった\:この場合、唯一の希望の動作になります、次の3つのプロパティを使用していますか? – matrix

答えて

1

私はこれのスタイルをstyles.xmlを使用することを好むだろう。しかし、次のようにプログラム的に行うことができること:

1.ハイライトカラーハイライトカラー

まず。これは、次を使用して設定することができます。

editText.setHighlightColor(color); 

2.左と右のマーク

左と右のマークがまだこので着色されていません。あなたの反射法を続けると、我々はこれらの選択マーカーのために同じの並べ替えを行う必要があります。

// Left 
Field fCursorDrawableLeftRes = TextView.class.getDeclaredField("mTextSelectHandleLeftRes"); 
fCursorDrawableLeftRes.setAccessible(true); 
int mCursorDrawableLeftRes = fCursorDrawableLeftRes.getInt(editText); 

// Right 
Field fCursorDrawableRightRes = TextView.class.getDeclaredField("mTextSelectHandleRightRes"); 
fCursorDrawableRightRes.setAccessible(true); 
int mCursorDrawableRightRes = fCursorDrawableRightRes.getInt(editText); 

とofcourseの:(あなたのソースから更新)それらを更新するために、ドローアブルリストに追加する:

Drawable[] drawables = new Drawable[3]; 
Resources res = editText.getContext().getResources(); 
drawables[0] = res.getDrawable(mCursorDrawableRes); 
drawables[1] = res.getDrawable(mCursorDrawableLeftRes); 
drawables[2] = res.getDrawable(mCursorDrawableRightRes); 
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
drawables[2].setColorFilter(color, PorterDuff.Mode.SRC_IN); 

3.結果

これはあなたの方法は次のようになりますことを意味します

private void setCursorDrawableColor(EditText editText, int color) { 
    try { 
     Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); 
     fCursorDrawableRes.setAccessible(true); 
     int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); 

     // Left 
     Field fCursorDrawableLeftRes = TextView.class.getDeclaredField("mTextSelectHandleLeftRes"); 
     fCursorDrawableLeftRes.setAccessible(true); 
     int mCursorDrawableLeftRes = fCursorDrawableLeftRes.getInt(editText); 

     // Right 
     Field fCursorDrawableRightRes = TextView.class.getDeclaredField("mTextSelectHandleRightRes"); 
     fCursorDrawableRightRes.setAccessible(true); 
     int mCursorDrawableRightRes = fCursorDrawableRightRes.getInt(editText); 

     Field fEditor = TextView.class.getDeclaredField("mEditor"); 
     fEditor.setAccessible(true); 
     Object editor = fEditor.get(editText); 
     Class<?> clazz = editor.getClass(); 
     Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); 
     fCursorDrawable.setAccessible(true); 

     Drawable[] drawables = new Drawable[3]; 
     Resources res = editText.getContext().getResources(); 
     drawables[0] = res.getDrawable(mCursorDrawableRes); 
     drawables[1] = res.getDrawable(mCursorDrawableLeftRes); 
     drawables[2] = res.getDrawable(mCursorDrawableRightRes); 
     drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     drawables[2].setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     fCursorDrawable.set(editor, drawables); 
    } catch (final Throwable ignored) {} 
} 
私が言ったように、私はこの動作を実現するためのstyles.xmlを使用することを好むだろう

styles.xmlを使用して)

//他の方法。あなたの代わりにリフレクションのスタイルにしてみてください(もちろん強調するためとtextColorHighlight

<item name="colorControlNormal">@android:color/holo_green_dark</item> 
<item name="colorControlActivated">@android:color/holo_green_dark</item> 
<item name="colorControlHighlight">@android:color/holo_green_dark</item> 

関連する問題