【问题标题】:How to set Android WebView background of selected textbox如何设置所选文本框的Android WebView背景
【发布时间】:2009-10-24 00:05:27
【问题描述】:

我在 Android WebView 的 HTML 页面中有一个输入标签。我希望背景是透明的。它是(通过background-color: transparent)直到用户选择它(给它焦点,输入数据)。然后背景是白色的。

我在textboxtextbox:focus 上尝试过background-color,我也尝试过-webkit-appearance: none。这些都不起作用。

有人搞过这个吗?

【问题讨论】:

  • Android 2.1 中似乎已修复此问题。

标签: android webkit


【解决方案1】:

我可能错了,但这实际上看起来像是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2013-12-26
    • 2015-07-13
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多