android webview 内嵌H5踩经历

前言

最近项目当中使用了webview 内嵌h5页面 ,一切完成后,测试人员告述说,无法调取输入法,纳尼?  心想这怎么可能 ,哈哈,不过真的是,这里把解决的贴出来

参考

https://blog.csdn.net/tu_bingbing/article/details/41810473

https://blog.csdn.net/damoguyun/article/details/75448087

解决办法

遇到了问题,我首先找了度娘,度娘告述我加上webview.requestFocus();  以为就这样简单了事,但是结果让我很意外,根本不好使。后来又使用了webview.requestFocusFromTouch() ;  但是发现依然不好使。后来看了前辈的文章,才知道,如果没有重新自定义webview这样的是没有问题的。但是自定义了webview以后,这样写就不好使了。

因为我的webView 中加入了进度条的显示, 所以,我重写了webview,这样一来,我的webview加入上面的就不好使了

public ProgressWebView(Context context) {
    this(context, null);
}

public ProgressWebView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ProgressWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

这构造方法中,就会执行defStyleAttr传入了0  ,这样webview  是不能正常执行的

所以我们在引入webview的地方引入了style

android:id="@+id/webview"
style="?android:attr/webViewStyle" //这句话很关键的 
android:layout_width="match_parent"
android:layout_height="match_parent"

这样,webview就能够正常使用了,然后  在构造方法当当中加入方法

public ProgressWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
    this.requestFocus();
    this.requestFocus(View.FOCUS_DOWN);
    this.requestFocusFromTouch();
}

这样就完美的解决了webview  中内嵌H5  不能调取输入法的问题

希望对您有帮助!!!谢谢

你可能感兴趣的:(Android,webview)