Set EditText cursor color

后端 未结 24 1404
醉话见心
醉话见心 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:30

    After a lot of time spent trying all these technique in a Dialog, I finally had this idea : attach the theme to the Dialog itself and not to the TextInputLayout.

    <style name="AppTheme_Dialog" parent="Theme.AppCompat.Dialog">
    
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorWhite</item>
        <item name="colorAccent">@color/colorPrimary</item>
    
    </style>
    

    inside onCreate :

    public class myDialog extends Dialog {

    private Activity activity;
    private someVars;
    
    public PopupFeedBack(Activity activity){
        super(activity, R.style.AppTheme_Dialog);
        setContentView(R.layout.myView);
        ....}}
    

    cheers :)

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

    For anyone that needs to set the EditText cursor color dynamically, below you will find two ways to achieve this.


    First, create your cursor drawable:

    <?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>
    

    Set the cursor drawable resource id to the drawable you created (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)):

    try {
        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
        f.setAccessible(true);
        f.set(yourEditText, R.drawable.cursor);
    } catch (Exception ignored) {
    }
    

    To just change the color of the default cursor drawable, you can use the following method:

    public static 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) {
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:34

    I found the answer :)

    I've set the Theme's editText style to:

    <item name="android:editTextStyle">@style/myEditText</item>
    

    Then I've used the following drawable to set the cursor:

    `

    <style name="myEditText" parent="@android:style/Widget.Holo.Light.EditText">
        <item name="android:background">@android:drawable/editbox_background_normal</item>
        <item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item>
        <item name="android:height">40sp</item>
    </style>
    

    `

    android:textCursorDrawable is the key here.

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

    The only valid answer should be to change the theme of the activity: <item name="colorAccent">#000000</item> You should not use the android:textCursorDrawable to @null because this only concerns the cursor itself but not the pin below the cursor if you want to drag that cursor. The theming solution is the most serious one.

    0 讨论(0)
  • 2020-11-22 11:34
    <resources>
    
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item> -- change this one
    </style>
    

    Go to styles.xml and change the color accent and this will influence the cursor from the edittext box

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

    Another simple solution would be to go to res>values>colors.xml in your project folder and edit the value of the color accent to the color you prefer

    <color name="colorAccent">#000000</color>
    

    The code above changes your cursor to black.

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