Android webview 无法弹出输入法

今天突然遇到一个问题,vue 写的 h5 页面无法弹出输入法,我查看了 vue 源码,确定没问题,那么,问题肯定是 Android 的了。于是查看了 AndroidManifest 没发现 windowSoftInputMode 的相关配置,kotlin 代码里也没有操作 windowSoftInputMode 的片段,那么,可以确定这是 webview 本身的问题了。

最后发现,我的代码是这样写的:

class ProgressWebView(private var mContext: Context?, attrs: AttributeSet) : LollipopFixedWebView(mContext, attrs, 0) {

没错,问题就在这里了,应该把后面的 , 0 去掉,改为:

class ProgressWebView(private var mContext: Context?, attrs: AttributeSet) : LollipopFixedWebView(mContext, attrs) {

至于这里的 LollipopFixedWebView 请参考:https://stackoverflow.com/questions/41025200/android-view-inflateexception-error-inflating-class-android-webkit-webview

具体来说就是 在 Android 5.0 系统上,你使用 webview 可能会报错:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003

这是因为 context 错了,具体为啥错了,我没去研究。具体的解决方案是 新建一个 LollipopFixedWebView,然后让你的 webview 继承它,LollipopFixedWebView 源码如下:

public class LollipopFixedWebView extends WebView {
    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);
    }

    public static Context getFixedContext(Context context) {
        return context.createConfigurationContext(new Configuration());
    }
}

如果使用 kotlin 那么 defStyleAttr 不要传 0 否则 输入法弹不出,可以考虑用多个构造函数。

本博客若无特殊说明则由 full-stack-trip 原创发布
转载请点名出处:全栈之旅 > Android webview 无法弹出输入法
本文地址:https://www.kpromise.top/android-webview-input-not-show/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注