关于webview使用过程中的一些坑

http://www.jb51.net/article/129250.htm

最近项目有使用到webview这个控件,遇到的问题比较多,看了挺多相关的问题,上面这篇博文写的挺好的,特此记录一下,感谢博主,也作为一个记录,后面也许能用上。

我遇到的两个更头痛的问题,现在还没有解决,我描述一下,希望大神能帮忙解答一下,感激不尽:

第一个问题:webview获取焦点之后,软键盘弹出来会把webview的输入框挡住,webview的内容不会自动弹上去

网上找了很久,无非就三个,设置软件盘弹出模式;设置webview以及其父控件的属性;还有一个呼声比较高的AndroidBug5497Workaround类的处理,相关的内容我就不粘贴了,直接给链接,有兴趣的可以看一下:

https://blog.csdn.net/sophiedjf/article/details/41512793;

http://www.barryzhang.com/archives/491

上面链接说到的解决方法一般是有效的,但是我这里不起作用,为什么呢?因为我这里的布局情况比较特殊,我的webview的是在ScrollView里面的,这也是后面才发现的,去掉ScrollView的话问题是可以解决的,但是需求在那,你就是不可以把ScrollView去掉,大概发一下布局内容吧:

<RelativeLayout
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.poso2o.lechuan.view.LazyScrollView
        android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/art_detail_web"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />

            <View style="@style/horizontal_line_gray" />

            <LinearLayout
                android:id="@+id/add_ad_layout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_tiny"
                android:gravity="center_vertical">

                <TextView
                    android:id="@+id/tag_ad"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_margin="@dimen/spacing_middle"
                    android:text="添加广告:"
                    android:textColor="@color/colorOrange"
                    android:textSize="@dimen/textSize_normal" />

                <TextView
                    android:id="@+id/ad_model_name"
                    style="@style/text_9E9E9E_14"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/tag_ad"
                    android:layout_weight="1"
                    android:hint="请选择广告模版" />

                <ImageView
                    android:id="@+id/show_ad_models"
                    android:layout_width="@dimen/height_tiny"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:padding="@dimen/spacing_middle"
                    android:src="@mipmap/arrow_less" />
            </LinearLayout>

            <View style="@style/horizontal_line_gray" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView_ad_model"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_small"
                android:background="@color/color_EEEEEE"
                android:gravity="center_vertical">

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="3" />

                <TextView
                    android:id="@+id/add_to_renewals"
                    android:layout_width="120dp"
                    android:layout_height="36dp"
                    android:background="@drawable/shape_per_reduce"
                    android:gravity="center"
                    android:text="加入草稿箱"
                    android:textSize="@dimen/z_text_small" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2" />

                <TextView
                    android:id="@+id/add_to_publish"
                    android:layout_width="120dp"
                    android:layout_height="36dp"
                    android:background="@drawable/selector_sign_btn"
                    android:gravity="center"
                    android:text="添加到发布"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/z_text_small" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="3" />

            </LinearLayout>

        </LinearLayout>

    </com.poso2o.lechuan.view.LazyScrollView>

    <ImageView
        android:id="@+id/art_to_bottom"
        android:layout_width="50dp"
        android:layout_height="36dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="36dp"
        android:src="@mipmap/arrow_more" />

</RelativeLayout>

第二个问题:调用JS获取加载的HTML的源码某些字符被转移符替换了,导致iOS 端把这些内容设置到它们的webview的时候就是一串似懂非懂的字符串,上一些基本的代码:

加载HTML

art_detail_web.loadUrl("http://editor/?v=3.0");

给HTML设置内容:设置的内容是非常正常的“<p>12346</p>”

art_detail_web.loadUrl("javascript:setHTML('" + str + "')");

再调用JS获取该HTML的源码:

art_detail_web.evaluateJavascript("getAllHtml()", new ValueCallback<String>()
然后获取到的HTML源码就成了"\u003Cp ...\">\u003C/p>"这样的,导致iOS端给webview.setHTML之后就是一串字符串显示,并不是我们所期望的网页,但是在Android这边显示这段内容最终还是可以显示成正常的网页的,这里就不明白了,iOS端也是调用同样的js方法,他们拿到的HTML源码跟设置的内容一样,有怀疑是不是编码问题,然后后面同意给webview设置了UTF-8的编码方式,还是不能解决这个问题,希望有经验的大神帮忙解答一下,感激不尽

猜你喜欢

转载自blog.csdn.net/dennyzhang2/article/details/79725214