Set EditText cursor color

后端 未结 24 1402
醉话见心
醉话见心 2020-11-22 10:57

I am having this issue where I am using the Android\'s Holo theme on a tablet project. However, I have a fragment on screen which has a white background. I am adding an

相关标签:
24条回答
  • 2020-11-22 11:26

    Use this

    android:textCursorDrawable="@color/white"
    
    0 讨论(0)
  • 2020-11-22 11:27

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#36f0ff</item>
        <item name="colorPrimaryDark">#007781</item>
        <item name="colorAccent">#000</item>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    

    change t he color of colorAccent in styles.xm, that's it simple

    0 讨论(0)
  • 2020-11-22 11:28

    Here @Jared Rummler's programatic setCursorDrawableColor() version adapted to work also on Android 9 Pie.

    @SuppressWarnings({"JavaReflectionMemberAccess", "deprecation"})
    public static void setCursorDrawableColor(EditText editText, int color) {
    
        try {
            Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
            cursorDrawableResField.setAccessible(true);
            int cursorDrawableRes = cursorDrawableResField.getInt(editText);
            Field editorField = TextView.class.getDeclaredField("mEditor");
            editorField.setAccessible(true);
            Object editor = editorField.get(editText);
            Class<?> clazz = editor.getClass();
            Resources res = editText.getContext().getResources();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                Field drawableForCursorField = clazz.getDeclaredField("mDrawableForCursor");
                drawableForCursorField.setAccessible(true);
                Drawable drawable = res.getDrawable(cursorDrawableRes);
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                drawableForCursorField.set(editor, drawable);
            } else {
                Field cursorDrawableField = clazz.getDeclaredField("mCursorDrawable");
                cursorDrawableField.setAccessible(true);
                Drawable[] drawables = new Drawable[2];
                drawables[0] = res.getDrawable(cursorDrawableRes);
                drawables[1] = res.getDrawable(cursorDrawableRes);
                drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
                drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
                cursorDrawableField.set(editor, drawables);
            }
        } catch (Throwable t) {
            Log.w(TAG, t);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:28

    editcolor.xml

    android:textCursorDrawable="@drawable/editcolor"

    In xml file set color code of edittext background color

    0 讨论(0)
  • 2020-11-22 11:28

    you can use code below in layout

    android:textCursorDrawable="@color/red"
            android:textColor="@color/black
    
    0 讨论(0)
  • 2020-11-22 11:29

    that's called colorAccent in Android.

    go to res -> values -> styles.xml add

    <item name="colorAccent">#FFFFFF</item>
    

    if not exists.

    0 讨论(0)
提交回复
热议问题