Skip to content

Commit

Permalink
fix(android): set text cursor of input for android 12
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and zoomchan-cxj committed Dec 15, 2021
1 parent 92809fc commit 6725483
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
Expand Up @@ -15,6 +15,9 @@
*/
package com.tencent.mtt.hippy.views.textinput;

import android.graphics.BlendMode;
import android.graphics.BlendModeColorFilter;
import androidx.core.content.ContextCompat;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.HippyInstanceContext;
import com.tencent.mtt.hippy.common.HippyMap;
Expand Down Expand Up @@ -622,47 +625,62 @@ public void setOnSelectListener(boolean change) {

@SuppressWarnings("JavaReflectionMemberAccess")
public void setCursorColor(int color) {
try {
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(this);
field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(this);
Drawable drawable = null;
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
drawable = this.getContext().getDrawable(drawableResId);
} else if (version >= 16) {
drawable = this.getContext().getResources().getDrawable(drawableResId);
}
if (drawable == null) {
return;
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
// Pre-Android 10, there was no supported API to change the cursor color programmatically.
// In Android 9.0, they changed the underlying implementation,
// but also "dark greylisted" the new field, rendering it unusable.
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Drawable cursorDrawable = getTextCursorDrawable();
if (cursorDrawable != null) {
cursorDrawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_IN));
setTextCursorDrawable(cursorDrawable);
}
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
assert editor != null;
Class<?> editorClass = editor
.getClass(); //有的ROM自己复写了,Editor类,所以之类里面没有mDrawableForCursor,这里需要遍历
while (editorClass != null) {
try {
if (version >= 28) {
field = editorClass.getDeclaredField("mDrawableForCursor");//mCursorDrawable
field.setAccessible(true);
field.set(editor, drawable);
} else {
Drawable[] drawables = {drawable, drawable};
field = editorClass.getDeclaredField("mCursorDrawable");//mCursorDrawable
field.setAccessible(true);
field.set(editor, drawables);
} else {
try {
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(this);
field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(this);
Drawable drawable = null;
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
drawable = this.getContext().getDrawable(drawableResId);
} else if (version >= 16) {
drawable = this.getContext().getResources().getDrawable(drawableResId);
}
if (drawable == null) {
return;
}
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
assert editor != null;
Class<?> editorClass = editor
.getClass(); //有的ROM自己复写了,Editor类,所以之类里面没有mDrawableForCursor,这里需要遍历
while (editorClass != null) {
try {
if (version >= 28) {
field = editorClass.getDeclaredField("mDrawableForCursor");//mCursorDrawable
field.setAccessible(true);
field.set(editor, drawable);
} else {
Drawable[] drawables = {drawable, drawable};
field = editorClass.getDeclaredField("mCursorDrawable");//mCursorDrawable
field.setAccessible(true);
field.set(editor, drawables);
}
break;
} catch (Throwable e) {
LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage());
}
break;
} catch (Throwable e) {
LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage());
editorClass = editorClass.getSuperclass(); //继续往上反射父亲
}
editorClass = editorClass.getSuperclass(); //继续往上反射父亲
} catch (Throwable e) {
LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage());
}
} catch (Throwable e) {
LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage());
}
}
}
Expand Up @@ -123,21 +123,12 @@ public void setEditable(HippyTextInput hippyTextInput, boolean editable) {

/**
* 设置输入光标颜色
**///RN 语法 caret-color
**/
@HippyControllerProps(name = "caret-color", defaultType = HippyControllerProps.NUMBER, defaultNumber = Color.TRANSPARENT)
public void setCaretColor(HippyTextInput hippyTextInput, int cursorColor) {
hippyTextInput.setCursorColor(cursorColor);
}

/**
* 设置输入光标颜色
**/
//For Vue.vue的前端语法,会把caret-color转化成caretColor
@HippyControllerProps(name = "caretColor", defaultType = HippyControllerProps.NUMBER, defaultNumber = Color.TRANSPARENT)
public void setCaretColorAlias(HippyTextInput hippyTextInput, int cursorColor) {
hippyTextInput.setCursorColor(cursorColor);
}

@HippyControllerProps(name = "multiline", defaultType = HippyControllerProps.BOOLEAN, defaultBoolean = true)
public void multiLine(HippyTextInput hippyTextInput, boolean multiline) {
int inputType = hippyTextInput.getInputType();
Expand Down

0 comments on commit 6725483

Please sign in to comment.