2014-09-23 59 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,你会使用编程方式创建一个
相关问题