Android Webview 自动完成文本颜色

发布于 2024-12-12 17:55:38 字数 3020 浏览 3 评论 0原文

我的应用程序中集成了一个 WebView。我面临的问题是当 WebView 识别到之前在文本字段中输入了一个值时会弹出自动完成功能。自动完成中的文本似乎是白色的。我没有向这些文本字段添加任何功能。网页按原样显示。有人知道为什么会发生这样的事情吗?有没有办法将文本颜色设置为黑色?

在所附的屏幕截图中,用户尝试在文本字段中输入电子邮件,然后弹出白色自动完成下拉列表。

以下代码显示了如何创建 Webview:

myxml.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id = "@+id/viewFrame"
android:background="@color/myColor">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_rectangle"
    android:layout_gravity="center"
    android:visibility="gone" 
    android:id  = "@+id/imageLayout">
    <ImageView android:id="@+id/loading_icon"
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:layout_height="wrap_content"
        android:src = "@drawable/loading_ic"/>
</RelativeLayout>
<WebView android:id="@+id/webview"
    android:scrollbars="none"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layerType="software" />
</FrameLayout>

MainActivity.java

 mWebView = (WebView) root.findViewById(R.id.webview);
 mWebView.setWebViewClient(mWebViewClient);
 mWebView.setWebChromeClient(mWebChromeClient);
 mWebView.getSettings().setJavaScriptEnabled(true);
 mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);

自动完成中的白色文本

编辑:解决方案

经过几个小时的研究,我终于弄清楚了问题所在。我在这里发布解决方案,以便它可能对可能面临类似问题的其他人有所帮助。据我了解,问题在于 WebView 如何替换网页上的输入字段。它使用 WebTextView(WebKit 的一部分)覆盖每个输入字段。此 WebTextView 是 Android.Widget.Autocomplete 的子级。 基本上,设置自动完成和 DropDownItem 的样式似乎是解决方案:

styles.xml

<style name="ActivityTheme" parent="android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:autoCompleteTextViewStyle">@style/Widget.AutoCompleteTextViewLight</item>
    <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
</style>


<style name="Widget.AutoCompleteTextViewLight" parent="@android:style/Widget.AutoCompleteTextView">
    <item name="android:textColor">@android:color/primary_text_light</item>
</style>

<style name="Widget.DropDownItemLight" parent="@android:style/Widget.DropDownItem">
    <item name="android:textColor">@android:color/primary_text_light</item>
</style>    

现在将 ActivityTheme 应用于清单中的活动,如下所示:

android:theme="@style/ActivityTheme"

< em>这个链接对帮助我解决问题很有帮助.

I have a WebView integrated into my application. The problem I am facing is with the autocomplete that pops up when the WebView recognizes that there was a value previously entered into the text field. The text in the autocomplete seems to be white in color. I haven't added any functionality to these textfields. The webpage is being displayed as is. Would anyone know why something like this is happening? Is there a way to set the color of the text to black?

In the screenshot attached, the user is trying to enter an email into the text field and the white autocomplete dropdown pops up.

The following code shows how I create the Webview:

myxml.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id = "@+id/viewFrame"
android:background="@color/myColor">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_rectangle"
    android:layout_gravity="center"
    android:visibility="gone" 
    android:id  = "@+id/imageLayout">
    <ImageView android:id="@+id/loading_icon"
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:layout_height="wrap_content"
        android:src = "@drawable/loading_ic"/>
</RelativeLayout>
<WebView android:id="@+id/webview"
    android:scrollbars="none"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layerType="software" />
</FrameLayout>

MainActivity.java

 mWebView = (WebView) root.findViewById(R.id.webview);
 mWebView.setWebViewClient(mWebViewClient);
 mWebView.setWebChromeClient(mWebChromeClient);
 mWebView.getSettings().setJavaScriptEnabled(true);
 mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);

White Text in AutoComplete

EDIT: Solution

After working on this for several hours, I finally figured out what the problem was. I am posting the solution here so that it might be helpful to others who might be facing a similar issue. The problem, as I understand is with how WebView replaces input fields on a webpage. It overlays each input field with a WebTextView (part of WebKit). This WebTextView is a child of Android.Widget.Autocomplete.
Basically, setting a style for autocomplete and DropDownItem seems to be the solution:

styles.xml

<style name="ActivityTheme" parent="android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:autoCompleteTextViewStyle">@style/Widget.AutoCompleteTextViewLight</item>
    <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
</style>


<style name="Widget.AutoCompleteTextViewLight" parent="@android:style/Widget.AutoCompleteTextView">
    <item name="android:textColor">@android:color/primary_text_light</item>
</style>

<style name="Widget.DropDownItemLight" parent="@android:style/Widget.DropDownItem">
    <item name="android:textColor">@android:color/primary_text_light</item>
</style>    

Now apply the ActivityTheme to the activity in the manifest as follows:

android:theme="@style/ActivityTheme"

This link was instrumental in helping me resolve the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文