2014-09-23 61 views
16

在android中,我們可以通過以下方式更改光標顏色:如何以編程方式更改android中的Edittext光標顏色?

android:textCursorDrawable="@drawable/black_color_cursor"

我們如何動態地做到這一點?

在我的情況下,我已將光標設置爲白色,但我需要更改黑色如何操作?

// Set an EditText view to get user input 
    final EditText input = new EditText(nyactivity); 
    input.setTextColor(getResources().getColor(R.color.black)); 
+0

後續[此鏈接](https://stackoverflow.com/questions/7238450/set-edittext-cursor-color) ,最好的去。 – Krantiz 2017-09-10 06:12:35

回答

56

使用一些反射的伎倆,我

的Java:

// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564 
Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 
f.setAccessible(true); 
f.set(yourEditText, R.drawable.cursor); 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid android:color="#ff000000" /> 

    <size android:width="1dp" /> 

</shape> 

這裏是您可以使用,並不需要一個XML的方法:

public static void setCursorColor(EditText view, @ColorInt int color) { 
    try { 
    // Get the cursor resource id 
    Field field = TextView.class.getDeclaredField("mCursorDrawableRes"); 
    field.setAccessible(true); 
    int drawableResId = field.getInt(view); 

    // Get the editor 
    field = TextView.class.getDeclaredField("mEditor"); 
    field.setAccessible(true); 
    Object editor = field.get(view); 

    // Get the drawable and set a color filter 
    Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId); 
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); 
    Drawable[] drawables = {drawable, drawable}; 

    // Set the drawables 
    field = editor.getClass().getDeclaredField("mCursorDrawable"); 
    field.setAccessible(true); 
    field.set(editor, drawables); 
    } catch (Exception ignored) { 
    } 
} 
+2

你的代碼工作正常! – user4152 2015-03-26 13:40:05

+2

謝謝,你是救命恩人。 – Baggers 2015-04-15 10:44:33

4
android:textCursorDrawable="@null" 

然後在應用程序:

final EditText input = new EditText(nyactivity); 
input.setTextColor(getResources().getColor(R.color.black)); 

Get from here

+0

對不起,但我沒有任何xml的那個怎麼辦。 – 2014-09-23 13:33:16

5

這是函數的一個重寫版本從@Jared Rummler用幾項改進:

  • 支持Android 4.0.x
  • 特殊getDrawable(Context, int)函數由於API 22及以上版本已棄用getDrawable(int)
private static final Field 
     sEditorField, 
     sCursorDrawableField, 
     sCursorDrawableResourceField; 

static { 
    Field editorField = null; 
    Field cursorDrawableField = null; 
    Field cursorDrawableResourceField = null; 
    boolean exceptionThrown = false; 
    try { 
     cursorDrawableResourceField = TextView.class.getDeclaredField("mCursorDrawableRes"); 
     cursorDrawableResourceField.setAccessible(true); 
     final Class<?> drawableFieldClass; 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
      drawableFieldClass = TextView.class; 
     } else { 
      editorField = TextView.class.getDeclaredField("mEditor"); 
      editorField.setAccessible(true); 
      drawableFieldClass = editorField.getType(); 
     } 
     cursorDrawableField = drawableFieldClass.getDeclaredField("mCursorDrawable"); 
     cursorDrawableField.setAccessible(true); 
    } catch (Exception e) { 
     exceptionThrown = true; 
    } 
    if (exceptionThrown) { 
     sEditorField = null; 
     sCursorDrawableField = null; 
     sCursorDrawableResourceField = null; 
    } else { 
     sEditorField = editorField; 
     sCursorDrawableField = cursorDrawableField; 
     sCursorDrawableResourceField = cursorDrawableResourceField; 
    } 
} 

public static void setCursorColor(EditText editText, int color) { 
    if (sCursorDrawableField == null) { 
     return; 
    } 
    try { 
     final Drawable drawable = getDrawable(editText.getContext(), 
       sCursorDrawableResourceField.getInt(editText)); 
     drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     sCursorDrawableField.set(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN 
       ? editText : sEditorField.get(editText), new Drawable[] {drawable, drawable}); 
    } catch (Exception ignored) { 
    } 
} 

private static Drawable getDrawable(Context context, int id) { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
     return context.getResources().getDrawable(id); 
    } else { 
     return context.getDrawable(id); 
    } 
} 
+0

不錯,但我認爲用ContextCompat.getDrawable(context,resId)''替換你自定義的'getDrawable(context,resId)'方法會更好 - 就像開箱即用的方法一樣;) – 2017-05-03 13:27:11

1

我們設法做到這一點:

  1. 創建只有一個EditText佈局文件和光標顏色以XML設置就可以了。
  2. 充氣它
  3. 使用的EditText,你會使用編程方式創建一個
相關問題