android动态设置Edittext光标颜色,实现水滴颜色

package com.massky.sraum.Util;

import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Field;

import androidx.core.graphics.drawable.DrawableCompat;

public class CursorUtil {
    /**
     * 代码设置光标颜色
     *
     * @param editText 你使用的EditText
     * @param color    光标颜色
     */
    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");
            Class<?> editorClazz = Class.forName("android.widget.Editor");
            Field fCursorDrawable = editorClazz.getDeclaredField("mCursorDrawable");

            fCursorDrawable.setAccessible(true);

//            Drawable[] drawables = new Drawable[2];
//            drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
//            drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
//            drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);//SRC_IN 上下层都显示。下层居上显示。
//            drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
//            fCursorDrawable.set(editor, drawables);

            //根据光标的resid获取到相应的drawable
            Drawable cursorDrawable = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
            if (cursorDrawable == null) {
                return;
            }
            //然后给cursorDrawable着色
            Drawable tintDrawable  = tintDrawable(cursorDrawable, ColorStateList.valueOf(color));
            //把着色后的cursorDrawable给editor的mCursorDrawable数组
            Drawable[] drawables = new Drawable[] {tintDrawable, tintDrawable};
            fCursorDrawable.set(editor, drawables);


        } catch (Throwable ignored) {
            ignored.getMessage();
        }
    }

    public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
        //给相应的drawable着色(v4包中的DrawableCompat着色)
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappedDrawable, colors);
        return wrappedDrawable;
    }


}

 

 

实现水滴:

在styles.xml里修改使用的主题,加入<item name="colorControlActivated">@color/YOUR_COLOR</item>,其中YOUR_COLOR替换为你在colors.xml中的颜色。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
设置EditText光标颜色,你可以使用以下方法: 1. 全局设置:在AndroidManifest.xml文件中找到`android:theme="@style/Theme"`,然后点击查看Theme的属性。如果有`<item name="colorAccent">@color/colorAccent</item>`,那么`@color/colorAccent`就是全局设置光标颜色。你可以将其修改为你想要的颜色即可。\[2\] 2. 局部设置:你可以使用以下代码来自定义光标颜色: ```java editText.setHighlightColor(Color.RED); // 设置光标颜色 ``` 你可以将`Color.RED`替换为你想要的颜色。\[2\] 3. 如果你想要禁止编辑状态下的光标显示,你可以使用以下代码: ```java editText.setFocusable(false); editText.setFocusableInTouchMode(false); ``` 如果你想要启用编辑状态下的光标显示,你可以使用以下代码: ```java editText.setFocusableInTouchMode(true); editText.setFocusable(true); editText.requestFocus(); ``` 这样就可以根据需要设置EditText光标颜色和编辑状态了。\[3\] #### 引用[.reference_title] - *1* *3* [修改EditText光标颜色](https://blog.csdn.net/asd199205/article/details/72841053)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [设置EditText光标颜色](https://blog.csdn.net/m0_49241867/article/details/123346470)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值