【发布时间】:2013-02-14 13:10:30
【问题描述】:
所以我将带有Holo Colors Generator 和Action Bar Style Generator 的Holo Theme 的样式更改为我自己的颜色。但是当我在编辑文本中选择文本时,所选位置的“标记”仍然是蓝色的。怎么改?
【问题讨论】:
标签: android android-edittext android-theme
所以我将带有Holo Colors Generator 和Action Bar Style Generator 的Holo Theme 的样式更改为我自己的颜色。但是当我在编辑文本中选择文本时,所选位置的“标记”仍然是蓝色的。怎么改?
【问题讨论】:
标签: android android-edittext android-theme
这里最糟糕的部分是找到该项目的“名称”以及如何在主题中调用它。所以我查看了android SDK文件夹中的每个drawable,最后找到了名为“text_select_handle_middle”、“text_select_handle_left”和“text_select_handle_right”的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>
【讨论】:
我知道这真的很晚了,但是如果您只想更改句柄的颜色,您只需将以下内容添加到您的 styles.xml 文件中。
<style name="ColoredHandleTheme">
<item name="colorControlActivated">@color/colorYouWant</item>
</style>
然后只需将主题设置为持有您想要影响的EditText 的任何活动。
或者,如果您想在应用范围内设置它,您可以执行以下操作:
<style name="ColoredHandleThemeForWholeApp">
<item name="colorAccent">@color/colorYouWant</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) {
}
【讨论】:
要更改选择手柄的颜色,您必须在应用主题中覆盖激活的颜色:
<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>
【讨论】:
您可以在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)了解其他解决方案。
【讨论】:
对于那些尝试通过代码处理此问题的人,您可以使用 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) {
}
}
【讨论】:
如果您只想在一个 EditText 上隐藏指针,您可以只为其设置主题:
styles.xml
<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" />
【讨论】: