我正在 android Webview 中加载一个网页,其中包含几个按钮:
<按钮 id="ID"class="class"type="submit">按钮标签
还有一个 JavaScript,当您单击按钮时应该触发它。
在 Android 4.4 之前的设备上一切正常。但在 4.4 上它什么也不做。
有没有办法解决这个问题并使其在 android 4.4 上也能工作?如果是这样,解决方案是什么?
这是网页 View 代码
private void init() {
webview = (WebView) getView().findViewById(R.id.webview);
webview.clearCache(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
currentUrl = url;
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(startUrl);
}
这是当我点击 web View 中的按钮时将调用的 javascript
{ window.open('<url which should be opened out of the app>');
self.location = "<webviewredirecturl>"; }
最佳答案
根据我的测试,当您的项目目标是 kitkat 时,loadUrl 方法的行为会发生变化。 它不会在当前页面上执行加载,而是系统地使用您的 url 参数打开一个新页面。
就我而言,我将此网址发送到我已加载的页面 webview:
javascript:showComment('Content html to add')
使用目标 API 18 一切正常,使用目标 API 19 时出现错误。
有关 kitkat she 的更改的更多信息:
http://www.mobilexweb.com/blog/android-4-4-kitkat-browser-chrome-webview
这里有类似的帖子: Loading javascript functions to webview in Android Kitkat
关于javascript - Android sdk 19 javascript 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933203/