How to Change programmatically Edittext Cursor Color in android?

前端 未结 9 420
眼角桃花
眼角桃花 2020-12-08 14:23

In android we can change the cursor color via:

android:textCursorDrawable=\"@drawable/black_color_cursor\".

How can we do this dynamically?

相关标签:
9条回答
  • 2020-12-08 15:16
    android:textCursorDrawable="@null"
    

    Then in the application:

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

    Get from here

    0 讨论(0)
  • 2020-12-08 15:17

    This is a rewritten version of the function from @Jared Rummler with a couple of improvements:

    • Support for Android 4.0.x
    • The special getDrawable(Context, int) function sience the getDrawable(int) is deprecated for API 22 and above.
    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 讨论(0)
  • 2020-12-08 15:18

    2019 Updated: working smooth and easy https://material.io/develop/android/docs/getting-started/

    If you are using material component just simply use textCursorDrawable with color or your custom drawable.

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="12dp">
    
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:textCursorDrawable="@color/red"
                android:cursorVisible="true"
                android:layout_height="wrap_content" />
    
        </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
提交回复
热议问题