【问题标题】:How to change color of EditText handles?如何更改 EditText 句柄的颜色?
【发布时间】:2015-04-22 10:49:13
【问题描述】:

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

Lollipop 应用的屏幕截图。

我怎样才能改变颜色:

  • 文本句柄drawables?如何给它们上色?
  • 全选背景颜色?

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

【问题讨论】:

标签: android android-edittext android-styles


【解决方案1】:

要更改句柄颜色,您可以按照here 指定的方式进行操作,您可以使用样式作为

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

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

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 设置为

android:textColorHighlight="#ff0000"

通过样式你可以做到

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

【讨论】:

  • 感谢您的帮助,我喜欢这两个答案,但您的答案更多的是关于文本处理程序。我想有没有办法只是着色它们?是的,也许先获取可绘制对象,然后对其进行着色...
【解决方案2】:

如下更改您的styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorControlHighlight">@color/accent_translucent</item>
    <item name="android:colorControlHighlight">@color/accent_translucent</item>
    <item name="colorAccent">@color/accent</item>
    <item name="selectableItemBackground">@drawable/selectable_item_background</item>
    <item name="android:selectableItemBackground">@drawable/selectable_item_background</item>

</style>

<color name="primary">#00BCD4</color>
<color name="primary_dark">#0097A7</color>
<color name="accent">#FFEB3B</color>
<color name="accent_translucent">#80FFEB3B</color>
<color name="accent_bright">#FFF493</color>

或者您可以在 EditText 中添加 XML 属性

android:textColorHighlight="@color/accent_translucent"

希望它能解决你的问题。

【讨论】:

  • 我也喜欢你的回答,因为它设置了整个应用程序的颜色。只是一件奇怪的事情我还没有弄清楚。我的一个edittexts 不接管主题,而另一个edittext 接管...您可能知道edittexts 不接管样式的情况吗?也许是错误的膨胀问题?
  • 谢谢@Lokkio。不,我没有遇到过这样的问题。这可能是由于膨胀问题。
猜你喜欢
  • 1970-01-01
  • 2013-02-14
  • 2023-03-17
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2016-12-04
相关资源
最近更新 更多