javascript 警报不是从 android 的 web 视图中出现的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6463151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:46:08  来源:igfitidea点击:

Alert is not appear from web view in android?

javajavascriptandroidandroid-webview

提问by Karthi

I use the sample provided by Googlefor demonstrating two way communication between JavaScript and Java ,

我使用Google提供的示例来演示 JavaScript 和 Java 之间的两种通信方式,

ref[1]:

参考文献[1]:

http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

The functionality is working fine. I am able to call JavaScript function from Java and callback the Java function from JavaScript.

该功能运行良好。我能够从 Java 调用 JavaScript 函数并从 JavaScript 回调 Java 函数。

The problem is when I use an alert inside a JavaScript function it won`t appear, but the functionality inside the function is working properly.

问题是当我在 JavaScript 函数中使用警报时,它不会出现,但函数内部的功能正常工作。

Why does alert("test")inside a JavaScript function not appear in Android. I load the JavaScript in a web-view. When I a clicking button in Android I call the function, but it does not appear.

为什么alert("test")在 Android 中没有出现 JavaScript 函数内部。我将 JavaScript 加载到web-view. 当我在 Android 中单击按钮时,我调用了该函数,但它没有出现。

If anyone knows the problem, pealse help me out.

如果有人知道这个问题,请帮助我。

Thanks

谢谢

回答by Android

setContentView(R.layout.main);
        WebView webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = webview.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webSettings.setBuiltInZoomControls(true);

        webview.requestFocusFromTouch();

        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());    

     webview.loadUrl("file:///android_asset/test.html");

this code working perfect and shows me alert box.. and this is my
test.html

这段代码完美运行并显示了警告框..这是我的
test.html

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

回答by Md Nawaz

By adding the following two lines, my JavaScript works:

通过添加以下两行,我的 JavaScript 可以工作:

mWebview.setWebViewClient(new WebViewClient()());
mWebview.setWebChromeClient(new WebChromeClient());

回答by Rohith Murali

Use the following mehtod,

使用以下方法,

WebView wv=new WebView(this);   
wv.setWebChromeClient(new WebChromeClient() {
@Override
    public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
    //Required functionality here
    return super.onJsAlert(view, url, message, result);
}

回答by Chandan kushwaha

Just use WebChromeClient. It will do everything.

只需使用 WebChromeClient。它会做一切。

mWebview.setWebChromeClient(new WebChromeClient());

It will work.

它会起作用。