我可能错了,但这实际上看起来像是 android.webkit 包代码中的一个错误。
即使在呈现的 HTML 中,WebView 似乎也使用 android.webkit.TextDialog 类来表示输入框。
但是,该代码使用 android 的文本小部件隐藏了 webkit 的呈现。请注意 LayerDrawable 变量层、对 setBackgroundDrawable() 的调用以及 LayerDrawable 中的两个层,其中底部设置为 always 为白色。
如您所见,WebCore 渲染的文本隐藏在这个白色背景后面。该评论甚至明确说明了这一点。
当然,WebView.java中的TextDialog变量mTextEntry在文本框被聚焦之前是不会被聚焦的,所以在那之前不会隐藏webkit正在渲染的东西。
/**
* Create a new TextDialog.
* @param context The Context for this TextDialog.
* @param webView The WebView that created this.
*/
/* package */ TextDialog(Context context, WebView webView) {
super(context);
mWebView = webView;
ShapeDrawable background = new ShapeDrawable(new RectShape());
Paint shapePaint = background.getPaint();
shapePaint.setStyle(Paint.Style.STROKE);
ColorDrawable color = new ColorDrawable(Color.WHITE);
Drawable[] array = new Drawable[2];
array[0] = color;
array[1] = background;
LayerDrawable layers = new LayerDrawable(array);
// Hide WebCore's text behind this and allow the WebView
// to draw its own focusring.
setBackgroundDrawable(layers);
// Align the text better with the text behind it, so moving
// off of the textfield will not appear to move the text.
setPadding(3, 2, 0, 0);
mMaxLength = -1;
// Turn on subpixel text, and turn off kerning, so it better matches
// the text in webkit.
TextPaint paint = getPaint();
int flags = paint.getFlags() | Paint.SUBPIXEL_TEXT_FLAG |
Paint.ANTI_ALIAS_FLAG & ~Paint.DEV_KERN_TEXT_FLAG;
paint.setFlags(flags);
// Set the text color to black, regardless of the theme. This ensures
// that other applications that use embedded WebViews will properly
// display the text in textfields.
setTextColor(Color.BLACK);
setImeOptions(EditorInfo.IME_ACTION_NONE);
}
git 中的source code。