gpt4 book ai didi

android - 以 29(Q) 作为目标 API 的 28(P) 的 EditText 句柄颜色

转载 作者:行者123 更新时间:2023-12-03 17:29:25 24 4
gpt4 key购买 nike

到目前为止,我一直在使用反射,因此更新了 editTexts 的选择句柄。在我更新为目标 api 29(Q) 之前,它工作正常。

谷歌似乎做了一些更新(或 Java,不完全确定),但我现在收到如下消息:

Accessing hidden field Landroid/widget/Editor;->mDrawableForCursor:Landroid/graphics/drawable/Drawable; (dark greylist, reflection)



从好的方面来说,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 的情况被 Google 的最新更新破坏了:(
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// insert working code here :D
}

我的问题,有没有人能够使用目标 api 29 以编程方式为在 api 28 上运行的设备设置句柄颜色?

为清楚起见,这是指的句柄:
Android EditText Handle

最佳答案

你不能用 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
}
}

关于android - 以 29(Q) 作为目标 API 的 28(P) 的 EditText 句柄颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com