如何更改EditText的颜色/外观选择句柄/锚点?

问题描述 投票:16回答:5

所以我用Holo Colors GeneratorAction Bar Style Generator将Holo Theme的风格改为我自己的颜色。但是当我在编辑文本中选择文本时,所选位置的“标记”仍为蓝色。我该怎么改变它?

android android-edittext android-theme
5个回答
34
投票

这里最糟糕的部分是找到这个项目的“名称”以及如何在主题中调用它。所以我查看了android SDK文件夹中的每个drawable,最后找到了名为“text_select_handle_middle”,“text_select_handle_left”和“text_select_handle_right”的drawable。

所以解决方案很简单:将这些带有自定义设计/颜​​色的drawable添加到drawable文件夹中,并将它们添加到主题样式定义中,如:

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

11
投票

如何从代码中执行此操作:

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) {
}

9
投票

我知道这已经很晚了,但如果您只想更改句柄的颜色,则只需将以下内容添加到styles.xml文件即可。

<style name="ColoredHandleTheme">
    <item name="colorControlActivated">@color/colorYouWant</item>
</style>

然后只需将主题设置为持有您想要影响的EditText的任何活动。

或者,如果要在应用程序范围内设置它,可以执行以下操作:

<style name="ColoredHandleThemeForWholeApp">
    <item name="colorAccent">@color/colorYouWant</item>
</style>

并为整个应用程序设置该主题。

问题解决了!


3
投票

要更改选择手柄的颜色,您必须覆盖应用主题中的激活颜色:

<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>

1
投票

你可以在http://androiddrawables.com/Other.html中看到这些属性。

例如,更改您的values / styles.xml:

<style name="AppTheme.Cursor" parent="AppTheme">
    <item name="colorAccent">@color/cursor</item>
</style>

其中@ color / cursor添加在values / color.xml中。之后将样式应用于活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme_Cursor);
    ...

访问How to change EditText pointer color (not cursor)获取其他解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.