选择文本时如何更改textview的光标颜色
How to change the cursor color of textview when selecting text
我试图在文本视图中选择文本时更改突出显示颜色和光标颜色。通过 Whosebug 帖子,我能够使用
更改突出显示颜色
textView.highlightColor = ContextCompat.getColor(context, R.color.red_1)
我想将光标颜色更改为自定义颜色。通过浏览大量 Whosebug 帖子,其中一个答案建议更改强调色。但我不想更改它,因为它会影响我应用程序中的很多 UI 属性。我找不到任何其他解决方案。
通过 Theme
定义 @style
来更改句柄 color
:
<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>
以编程方式进行:
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_green</item>
我试图在文本视图中选择文本时更改突出显示颜色和光标颜色。通过 Whosebug 帖子,我能够使用
更改突出显示颜色textView.highlightColor = ContextCompat.getColor(context, R.color.red_1)
我想将光标颜色更改为自定义颜色。通过浏览大量 Whosebug 帖子,其中一个答案建议更改强调色。但我不想更改它,因为它会影响我应用程序中的很多 UI 属性。我找不到任何其他解决方案。
通过 Theme
定义 @style
来更改句柄 color
:
<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>
以编程方式进行:
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_green</item>