解决demo如下
创建个activity
关键代码是webSetting中的三行代码:
javaScriptEnabled=true//支持javascript
javaScriptCanOpenWindowsAutomatically = true //支持openWindow
setSupportMultipleWindows(true)// 设置允许开启多窗口
package com.study.myapplication.ui
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Message
import android.util.Log
import android.webkit.*
import com.study.myapplication.R
import kotlinx.android.synthetic.main.acitivty_web.*
/**
* Created by xwg on 2020/2/27.
* describe TODO
*
*/
class WebActivity : Activity() {
internal val homeUrl = "file:///android_asset/test5.html"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.acitivty_web)
webView.settings.run {
javaScriptEnabled=true//支持javascript
javaScriptCanOpenWindowsAutomatically = true //支持openWindow
setSupportMultipleWindows(true)// 设置允许开启多窗口
}
webView.loadUrl(homeUrl)
webView.webChromeClient=object :WebChromeClient(){
override fun onCloseWindow(window: WebView?) {
super.onCloseWindow(window)
}
override fun onCreateWindow(view: WebView?, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message?): Boolean {
Log.e("window create","window create!!")
val newWindow= WebView(this@WebActivity)
newWindow.webViewClient= object :WebViewClient(){
override fun shouldOverrideUrlLoading(newWeb: WebView?, request: WebResourceRequest?): Boolean {
newWeb?.loadUrl(request!!.url.toString())
return true
}
}
newWindow.settings.javaScriptEnabled=true
newWindow.webChromeClient=this
content.addView(newWindow)
resultMsg?.obj?.let {
val transport = it as (WebView.WebViewTransport)
transport.webView=newWindow
resultMsg.sendToTarget()
}
Log.e("createWindow","createWindow")
return true
}
}
webView.webViewClient=object :WebViewClient(){
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
request?.url?.toString().let { url ->
if (!url!!.startsWith("http://") && !url.startsWith("https://")) {
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
} catch (e: Exception) {
e.printStackTrace()
return true
}
} else {
webView.loadUrl(url)
return true
}
}
return true
}
}
}
}
布局文件:acitivty_web.xml
这里关键父容器要FrameLayout或者relativeLayout ,这样可以用新的webview覆盖在老的webview上
实现动态添加,网上很多文章都没有提到这一点
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
</FrameLayout>
网络权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
js代码 test5.html
<html>
<head>
<meta charset="utf-8">
<title>test5</title>
<script>
function openWin(){
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>这是'我的窗口'</p>");
myWindow.focus();
}
</script>
</head>
<body>
<br/>
<button type="button" style="margin-top:30px;width:260px;font-size:30px" οnclick="openWin()">test1</button>
</body>
</html>
webview的弹窗不像 网页 直接弹出一个窗口,这个只能新建个webview覆盖在原来的webview,比较鸡肋,
这里推荐一个很好的测试网站
https://www.html.cn/demo/window-tongxin/