android - 如何更改 EditText 句柄的颜色?

标签 android android-edittext android-styles

绿色从何而来?我试过更改 accentColor 属性,该属性在应用程序的其他部分有效,但在此处无效。

editText Colors Lollipop 应用程序的屏幕截图。

如何更改颜色:

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

也许还有一些关于 future 的建议……你是怎么知道的?我一直遇到所有这些令人困惑的颜色/样式问题。某处是否有某种列表告诉我,我需要为某个组件覆盖哪种默认颜色或属性?

最佳答案

要更改 handle 颜色,您可以按照指定的方式进行 here你使用 style 作为

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

关于android - 如何更改 EditText 句柄的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795197/

相关文章:

android - android c native 应用程序和android应用程序之间的通信

android - 在具有 MenuItem.SHOW_AS_ACTION_ALWAYS 的 ActionBarSherlock 中将 TextWatcher 添加到 EditText

listview - 更改 Android 上的 Xamarin.Forms Listview 中的默认 TextColor

android - AlertDialog 类转换异常

android - 如何将多个数据从 fragment 传递到 Activity

android - Android EditText 的 getSelectionEnd() getSelectionStart() 不返回正确的值

android - AppCompatActivity 的 TextView 颜色始终为白色

Android 删除操作栏

android - Android 管理 api EMM 中来自企业的域和域用户

java - 从 SD 卡中读取 .txt 并插入以编辑文本