首页 > 解决方案 > 当文本编辑字段获得焦点时,Android Webview 不显示键盘

问题描述

我有 DialogFragment 和一个 WebView ,它打开了 Google Forms 网页。但是单击文本输入字段时键盘不显示。有什么建议么?原始 Android 8.1 (Nexus 5x)。

WebView webView = binding.webView;
webView.requestFocus(View.FOCUS_DOWN);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(google_form_url);

布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data />

    <WebView
        android:id="@+id/webView"
        style="?android:attr/webViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true" />

</layout>

显现:

<activity
            android:name="com.keeple.april.Activity2"
            android:theme="@style/AppTheme"
            android:screenOrientation="portrait"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustResize"/>

标签: androidwebviewgoogle-forms

解决方案


您是否尝试在 requestFocus 下方添加触摸侦听器?

webView.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus())
                {
                    v.requestFocus();
                }
                break;
        }
        return false;
    }
});

推荐阅读