当我尝试显示从WebView
中的API收到的HTML页面时遇到问题。
似乎没有加载Javascript和CSS。
此外,当我单击某些按钮时,我可以导航到其他页面,但是如果单击其他按钮,则什么都没有发生。
我已经尝试在WebView
中启用Javascript。
变量webPage包含HTML代码。
这是我的代码:
@SuppressLint("SetJavaScriptEnabled")
private fun showGarminDiag(webPage : String)
{
val dialogView = View.inflate(context, R.layout.dialog_garmin_connect, null)
val dialog = Dialog(context, R.style.MyAlertDialogStyle)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(dialogView)
val mWebView = dialogView.findViewById<WebView?>(R.id.garmin_connect_webview)
//val mWebView = dialogView.findViewById<AdvancedWebView?>(R.id.garmin_connect_webview)
val back = dialogView.findViewById<Button?>(R.id.webview_back)
val forward = dialogView.findViewById<Button?>(R.id.webview_forward)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mWebView?.settings?.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
//mWebView?.settings?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
}
mWebView?.settings?.javaScriptEnabled = true
mWebView?.settings?.javaScriptCanOpenWindowsAutomatically = true
mWebView?.loadData(webPage, "text/html", "UTF-8")
back?.setOnClickListener({
if (mWebView != null && mWebView.canGoBack())
mWebView.goBack()
})
forward?.setOnClickListener({
if (mWebView != null && mWebView.canGoForward())
mWebView.goForward()
})
mWebView?.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
val test = url
Toast.makeText(context, url, Toast.LENGTH_LONG).show()
if (url == "success")
{
getProfile()
}
val t = 0
}
override fun onFormResubmission(view: WebView?, dontResend: Message?, resend: Message?) {
super.onFormResubmission(view, dontResend, resend)
val test = dontResend
val test1 = resend
val t = 0
}
}
dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/garmin_connect_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp">
<Button
android:id="@+id/webview_back"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Retour"
android:background="@drawable/blue_all_rounded_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/webview_forward"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Avancer"
android:background="@drawable/blue_all_rounded_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<WebView
android:id="@+id/garmin_connect_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
<im.delight.android.webview.AdvancedWebView
android:id="@+id/garmin_connect_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
-->
</LinearLayout>
这里是一个链接,以查看变量webPage包含的内容的示例: https://codeshare.io/5e4ZyJ