【问题标题】:html textfield in WebView in an Android application is hidden by the soft keyboardAndroid应用程序中WebView中的html文本字段被软键盘隐藏
【发布时间】:2011-02-13 08:51:01
【问题描述】:

我有一个 Android 应用程序,它是一个带有 WebView 的 TabHost。我用它来加载一个特定的 html 文件,该文件的底部有一个文本字段。

当我触摸 html 文本框时,软键盘会弹出,并隐藏文本框,这样我就看不到我输入的内容了。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <TabWidget
            android:focusableInTouchMode="false"
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="63dp" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/layout"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
                <WebView
                    android:id="@+id/webview"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent"
                    android:layout_weight="1" />
            </LinearLayout>
        </FrameLayout>      
    </LinearLayout>
</TabHost>

我尝试使用android:windowSoftInputMode="adjustResize" 配置AndroidManifest.xml 文件,但没有成功。我还尝试用ScollView 替换布局中的FrameLayout,但这导致我的webview 在应用程序运行时无限增大。这可能是由于我在页面上运行了一些javascript。

我注意到android的网络浏览器有一个漂亮的行为——在网页中,软键盘弹出后,网页平滑滚动,用户可以看到可聚焦的文本字段。我的应用程序中怎么会有这种行为?

【问题讨论】:

  • 解决此问题的方法可能是在出现软键盘时收到通知,然后手动调整 web 视图的大小。我已经尝试在我的所有布局类中实现 onCreateInputConnection(EditorInfo),但是当键盘出现时无法收到通知。救命!

标签: android keyboard webview textfield


【解决方案1】:

我找到了解决此问题的方法。 (大约在我发布问题一周后;我今天才在 stackoverflow 中回答......)

我的代码中有一些片段改变了 WebView 的高度(为 TabBar 留出空间)。事实证明,当您在 WebView 上调用 setLayoutParams 时,即使您设置了 android:windowSoftInputMode="adjustResize",它也不会再改变其高度。

我通过将 TabBar 添加到 main.xml 布局文件中来规避更改 WebView 高度的需要,初始大小为 0。当我增加 TabBar 的大小时,WebView 的大小会自动减小,并保留 @987654322 @行为。

【讨论】:

  • 不知道这是否是WebView的一个已知bug?偶然发现了同样的问题
  • 我以前没有为此问题提交错误。那时我正在使用的 Android API 版本是 1.6 或 2.2(我不记得了)
  • 我有一个更复杂的布局,所以修复起来更难,但最后我做了和你一样的事情,并规避了为 webView 调用具有固定高度的 setLayoutParams
  • 你能用你的 webview 显示代码你是如何处理setLayoutParams() 的吗?我真的被这个问题困住了,还找不到解决方案。请更新您的答案。
【解决方案2】:

我快疯了android:windowSoftInputMode="adjustResize" 可能会有所帮助,但请确保您的应用不要全屏显示。

为我的应用删除全屏解决了使用软键盘调整布局大小的问题。

<item name="android:windowFullscreen">false</item>

【讨论】:

  • 请注意,如果 windowFullscreen 设置为 trueadjustResize 将不起作用
  • 我很确定他是这么说的。
猜你喜欢
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多