如何更改 EditText 句柄的颜色? [英] How to change color of EditText handles?

查看:57
本文介绍了如何更改 EditText 句柄的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绿色从何而来?我曾尝试更改 accentColor 属性,该属性适用于应用程序的其他部分,但不适用于此处.

Where does the green color comes from? I've tried changing the accentColor attribute, which works in other parts of the app, but not here.

Lollipop 中应用的屏幕截图.

Screenshot from app in Lollipop.

如何更改颜色:

  • 文本句柄可绘制对象?如何给它们着色?
  • 全选背景颜色?

也许还有一些对未来的提示......你是怎么发现的?我一直在遇到所有这些令人困惑的颜色/样式问题.是否有某种列表可以告诉我,我需要为某个组件覆盖哪个默认颜色或属性?

And maybe some tips for the future... How did you find out? I've been coming across all these confusing color/styling problems. Is there some kind of list somewhere that tells me, which default color or attribute that I need to overwrite for a certain component?

推荐答案

要更改手柄颜色,您可以按照指定操作 here 你使用 style as

For changing the handle color you can do as specified here you do it using style as

<style name="MyCustomTheme" parent="@style/MyNotSoCustomTheme">
        <item name="android:textSelectHandle">@drawable/text_select_handle_middle</item>
        <item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item>
        <item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item>
</style>

为了以编程方式检查相同的问题,另一个回复这里是使用反射完成的

For doing it programmatically check same question another reply here which is done using reflection

try {
    final Field fEditor = TextView.class.getDeclaredField("mEditor");
    fEditor.setAccessible(true);
    final Object editor = fEditor.get(editText);

    final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
    final Field fSelectHandleRight = editor.getClass().getDeclaredField("mSelectHandleRight");
    final Field fSelectHandleCenter = editor.getClass().getDeclaredField("mSelectHandleCenter");

    fSelectHandleLeft.setAccessible(true);
    fSelectHandleRight.setAccessible(true);
    fSelectHandleCenter.setAccessible(true);

    final Resources res = context.getResources();

    fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.text_select_handle_left));
    fSelectHandleRight.set(editor, res.getDrawable(R.drawable.text_select_handle_right));
    fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.text_select_handle_middle));
} catch (final Exception ignored) {
}

要更改选定的文本颜色,您可以将 xml 中的 textColorHighlight 设置为

For changing the selected text color you can set textColorHighlight in xml as

android:textColorHighlight="#ff0000"

通过你可以做的风格

<item name="android:textColorHighlight">@color/m_highlight_blue</item>

这篇关于如何更改 EditText 句柄的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆