Solve the demo below
Creating an activity
The key code is the webSetting in three lines of code:
javaScriptEnabled = true // support JavaScript
javaScriptCanOpenWindowsAutomatically = to true // support openWindow
setSupportMultipleWindows (to true) // settings allow open multiple windows
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
}
}
}
}
Layout File: acitivty_web.xml
Here the key to the parent container FrameLayout or relativeLayout, so you can use the new cover on webview webview old
Adding dynamic, online many articles do not mention this
<?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>
Network permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
js code 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 of direct pop up a window like pages, this can only be a new webview webview covered in the original, relatively tasteless,
Here recommend a good test site
https://www.html.cn/demo/window-tongxin/