我使用Holo Colors Generator和Action Bar Style Generator更改了Holo主题的样式,使其符合我的颜色。但是当我选择编辑文本中的文本时,所选位置上的“标记”仍然是蓝色的。我该如何更改它呢?
我使用Holo Colors Generator和Action Bar Style Generator更改了Holo主题的样式,使其符合我的颜色。但是当我选择编辑文本中的文本时,所选位置上的“标记”仍然是蓝色的。我该如何更改它呢?
<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) {
}
我知道这已经很晚了,但如果你只想改变手柄的颜色,你只需要在styles.xml文件中添加以下内容。
<style name="ColoredHandleTheme">
<item name="colorControlActivated">@color/colorYouWant</item>
</style>
接着,在包含你想要改变的EditText
的活动中设置主题即可。
或者,如果你想要全局应用这个主题,你可以按照以下步骤进行:
<style name="ColoredHandleThemeForWholeApp">
<item name="colorAccent">@color/colorYouWant</item>
</style>
将该主题设置为整个应用程序的主题。
问题解决了!
要更改选择手柄的颜色,您需要在应用程序主题中覆盖激活的颜色:
<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>
<style name="AppTheme.Cursor" parent="AppTheme">
<item name="colorAccent">@color/cursor</item>
</style>
在values/color.xml中添加@color/cursor,然后将样式应用于活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme_Cursor);
...
访问如何更改EditText指针颜色(而不是光标)以获取其他解决方案。
<style name="TransparentPointerEditText">
<item name="colorControlActivated">@android:color/transparent</item>
</style>
使用方法
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/TransparentPointerEditText" />
对于那些试图从代码中处理此问题的人,您可以使用Jared Rummler提供的答案来处理API版本小于或等于28的情况,但是从Android Q(API 29)开始,他们添加了一个@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)注释,并且在Q+上似乎不起作用。相反,EditText添加了新的方法:
setTextSelectHandle(R.drawable.test);
setTextSelectHandleLeft(R.drawable.test);
setTextSelectHandleRight(R.drawable.test);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
setTextSelectHandle(R.drawable.test);
setTextSelectHandleLeft(R.drawable.test);
setTextSelectHandleRight(R.drawable.test);
} else {
try {
final Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
final Object editor = fEditor.get(this);
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 = getResources();
fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.test, null));
fSelectHandleRight.set(editor, res.getDrawable(R.drawable.test, null));
fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.test, null));
} catch (final Exception ignored) {
}
}