点击webview中的输入框时,Android键盘未显示

23

我在我的Android应用中实现了一个自定义webview。如果我在这个webview中触摸输入框或文本区域,软键盘不会弹出。我没有在webview中重写任何touch listener,也没有在清单文件中更改任何内容。有人能帮我找出为什么键盘不显示的原因吗?

我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <nl.AEGEE.enschede.android.AEGEEWebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressTint="@color/aegee_blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-6dp"
        android:layout_alignParentTop="true" />

</RelativeLayout>
3个回答

30
请尝试将以下几行代码添加到 XML 文件中的 Webview 中。
android:focusable="true"
android:focusableInTouchMode="true"

希望能帮到你。


非常感谢,这就是全部!奇怪的是,在我的代码中扩展了一个WebView,没有自定义WebView时一切都按预期工作。 - Victor
3
在Web界面上,对于Google表单而言无法正常工作。 - Konstantin Konopko
@KonstantinKonopko 你是如何解决它的? - Yusuf Eka Sayogana

6

在我的情况下,除了添加

android:focusable="true"
android:focusableInTouchMode="true"

我还需要子类化 WebView 并添加此方法。
@Override
public boolean onCheckIsTextEditor() {
    return true;
}

我从这个帖子中得到了解决方案,其中提出了许多其他的解决方案。

这是整个WebView子类,如果有人想使用它

public class CustomWebView extends WebView {
    public CustomWebView(Context context) {
        super(context);

        init();
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    protected void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        BaseInputConnection baseInputConnection = new BaseInputConnection(this, false);
        outAttrs.imeOptions = IME_ACTION_DONE;
        outAttrs.inputType = TYPE_CLASS_TEXT;
        return baseInputConnection;
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }
}

1
为了使其正常工作,我不得不注释掉 onCreateInputConnection() 方法。 - ban-geoengineering
还有更多的构造函数需要添加:https://developer.android.com/reference/android/webkit/WebView#pubctors - ban-geoengineering

1
setFocusable(true)
setFocusableInTouchMode(true)

6
请在您的回答中提供某种解释,否则它将被标记为低质量。 - Mahib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接