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

jim*_*pic 18 android android-theme android-edittext

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

剩下中间对

jim*_*pic 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>
Run Code Online (Sandbox Code Playgroud)


Jar*_*ler 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) {
}
Run Code Online (Sandbox Code Playgroud)

  • @nicopasso,https://gist.github.com/jaredrummler/2317620559d10ac39b8218a1152ec9d4您可以通过删除两个数组中的左右字段名称来更改该方法以仅为中心句柄着色. (3认同)
  • 是的,这是可能的方法之一.但反思不是好主意) (2认同)

Luk*_*ner 9

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

<style name="ColoredHandleTheme">
    <item name="colorControlActivated">@color/colorYouWant</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

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

<style name="ColoredHandleThemeForWholeApp">
    <item name="colorAccent">@color/colorYouWant</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

问题解决了!

  • 我认为将 colorControlActivated 直接设置为 EditText 时不起作用。仅适用于活动级别(通过清单)。在搭载 Android 6 的 Nexus 5 和搭载 Android 4.4.4 的 Nexus 4 上进行测试。 (3认同)

Meh*_*ert 7

为了改变选择手柄的颜色,你必须在你的应用主题中覆盖激活的颜色:

<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

  • 它在特定的 EditText 上对我不起作用。仅在活动级别(使用清单)。你可以尝试一下吗? (3认同)
  • 有什么方法可以使它仅适用于活动中的特定 EditText,而不是全部? (2认同)

归档时间:

查看次数:

12565 次

最近记录:

6 年,8 月 前