到目前为止,我一直在使用反射,因此更新了 editTexts 的选择句柄。在我更新到目标 api 29(Q) 之前,它一直运行良好。
Google 似乎做了一些更新(或 Java,不完全确定),但我现在收到如下消息:
访问隐藏字段Landroid/widget/Editor;->mDrawableForCursor:Landroid/graphics/drawable/Drawable; (深灰名单,反射)
从好的方面来说,api 29(许多年后)有一种稳定的方式来以编程方式设置句柄颜色。不幸的是,它的向后兼容速度不如我发现的那么快,而且它还破坏了 api 28 的功能。api 28 以下的任何东西都可以通过反射正常工作。在我的代码下方,现在适用于除 api 28 以外的任何东西
/**
* Sets the color for the cursor and handles on the {@link EditText editText}.
*
* @throws EditTextTintError if an error occurs while tinting the view.
*/
public void apply() throws EditTextTintError {
try {
// Get the editor
Field field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(editText);
if (cursorColor != null) {
editText.setHighlightColor(ColorUtils.setAlphaComponent(cursorColor, 40));
// Get the cursor drawable, tint it, and set it on the TextView Editor
// Get the cursor resource id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ColorFilter colorFilter = new BlendModeColorFilter(cursorColor, BlendMode.SRC_ATOP);
editText.getTextCursorDrawable().mutate().setColorFilter(colorFilter);
editText.getTextSelectHandle().mutate().setColorFilter(colorFilter);
editText.getTextSelectHandleLeft().mutate().setColorFilter(colorFilter);
editText.getTextSelectHandleRight().mutate().setColorFilter(colorFilter);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
Field fieldP = TextView.class.getDeclaredField("mCursorDrawableRes");
fieldP.setAccessible(true);
int drawableResId = fieldP.getInt(editText);
// Get the editor
fieldP = TextView.class.getDeclaredField("mEditor");
fieldP.setAccessible(true);
Object editorP = fieldP.get(editText);
// Get the drawable and set a color filter
Drawable drawable = ContextCompat.getDrawable(editText.getContext(), drawableResId);
drawable.setColorFilter(cursorColor, PorterDuff.Mode.SRC_ATOP);
// Set the drawables
fieldP = editorP.getClass().getDeclaredField("mDrawableForCursor");
fieldP.setAccessible(true);
fieldP.set(editorP, drawable);
} catch (final Exception ignored) {
ignored.printStackTrace();
}
} else {
String[] resFieldNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};
String[] drawableFieldNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
Integer[] colors = {selectHandleLeftColor, selectHandleRightColor, selectHandleMiddleColor};
for (int i = 0; i < resFieldNames.length; i++) {
Integer color = colors[i];
if (color == null) {
continue;
}
String resFieldName = resFieldNames[i];
String drawableFieldName = drawableFieldNames[i];
field = TextView.class.getDeclaredField(resFieldName);
field.setAccessible(true);
int selectHandleRes = field.getInt(editText);
Drawable selectHandleDrawable = ContextCompat.getDrawable(editText.getContext(), selectHandleRes).mutate();
selectHandleDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
field = editor.getClass().getDeclaredField(drawableFieldName);
field.setAccessible(true);
field.set(editor, selectHandleDrawable);
}
}
}
} catch (Exception e) {
throw new EditTextTintError("Error applying tint to " + editText, e);
}
}
基本上 Android P 的外壳被谷歌的最新更新破坏了:(
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// insert working code here :D
}
我的问题,有没有人能够使用目标 api 29 为在 api 28 上运行的设备以编程方式设置句柄颜色?
你不能用
textField.setTextSelectHandle(drawable)
这样做吗?
对于 api < 29
// This avoids crash for api < 29: java.lang.NoSuchMethodError: No virtual method setTextSelectHandle
private void setTextSelectHandle(@NonNull EditText editText, @NonNull Context context) {
try {
final Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
final Object editor = fEditor.get(editText);
if (editor != null) {
final Field fSelectHandleCenter = editor.getClass()
.getDeclaredField("mSelectHandleCenter");
fSelectHandleCenter.setAccessible(true);
fSelectHandleCenter.set(editor, context.getDrawable(R.drawable.disable_text_handle));
}
} catch (ReflectiveOperationException exception) {
// no-op
}
}