WebView的OnCreateWindow是否可以制作弹出式窗口(Dialog风格)? 像安卓浏览器一样[英] possible WebView OnCreateWindow to make popup window(Dialog style)? like android browser

本文是小编为大家收集整理的关于WebView的OnCreateWindow是否可以制作弹出式窗口(Dialog风格)? 像安卓浏览器一样的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

当我在我的webview上用eBay.com上使用此代码按下按钮:

按钮的HTML:

input id="addPicturesBtn" name="addPicturesBtn" type="button" value="Add pictures" class="button" onclick="ebay.run(this.id,'onclick');return false;" onkeydown="ebay.run(this.id,'onkeydown');return false;" title="Add pictures"

如果在我的WebView中按下此按钮,它将从我的WebView中发送新窗口到Android浏览器.但如果从Android浏览器中按下相同的按钮,它会弹出此酷对话框类型弹出窗口(参见图片)

我想在ebay上打开弹出式样式窗口,如浏览器,如果在我的webview中按下此按钮.这样它就可以通过用户关闭,以便在使用弹出窗口时将它们返回到我的应用程序后面.

是可能的吗?

这是我到目前为止的内容:

   webChromeClient = new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        WebView childView = new WebView(Ebay.this);
        final WebSettings settings = childView.getSettings();
        settings.setJavaScriptEnabled(true);
        childView.setWebChromeClient(this);
        childView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();

    return true;
    }
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
            result.confirm();
            return true;

       }
我错过了什么?

这是Android浏览器弹出的图片(我试图让我的WebView从eBay.com上的按钮上启动):

popup

推荐答案

使用JavaScript接口.将JavaScript与Java合并,并在Java中进行自定义对话框 首先,启用JavaScript

webview.getSettings().setJavaScriptEnabled(true);

第二,您需要添加JavaScript接口

wv.addJavascriptInterface(new DemoJavaScriptInterface(), "js");

第三,加载URL;

webview.loadUrl("javascript:[javascript function name here]()");

这里是一些代码,希望这可以帮助 https://github.com/scottagarman/android-javascript-interface-example

代码信用转到名为"scottagarman"的开发人员.

其他推荐答案

你可以做到他的方式:

public void onCreate(Bundle savedInstanceState){
  ....
  webView.getSettings().setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new MyJSInterface(), "myappname");
  webView.loadUrl("http://mysite.com/"); // or some local asset
  ....
}

public class MyJSInterface{

....
 public void popup(){

//  AlterDialog etc.

 }
....

}


HTML from your website:

window.onload = function(){
   window.myappname.popup();
}

我希望它对你有用...

顺便说一句,要小心2.3版本它有一个相应的 bug ,只是fyi))

本文地址:https://www.itbaoku.cn/post/102701.html

问题描述

When I press a button with this code on ebay.com in my webview:

html of button:

input id="addPicturesBtn" name="addPicturesBtn" type="button" value="Add pictures" class="button" onclick="ebay.run(this.id,'onclick');return false;" onkeydown="ebay.run(this.id,'onkeydown');return false;" title="Add pictures"

If this button is pressed inside my webview, It sends the new window from my webview to the android browser. But if the same button is pressed from within the android browser it pops up this cool dialog type popup window (see picture)

I would like to open the popup style window on eBay like the browser does, if this button is pressed inside my webview. So that it can be closed by the user to return them to my app behind it when they are done with the popup.

Is that possible?

Here is what I have so far:

   webChromeClient = new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        WebView childView = new WebView(Ebay.this);
        final WebSettings settings = childView.getSettings();
        settings.setJavaScriptEnabled(true);
        childView.setWebChromeClient(this);
        childView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();

    return true;
    }
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
            result.confirm();
            return true;

       }

Am I missing something?

Here is a picture of the android browser popup (That I'm trying to get my webview to launch from the button on ebay.com):

popup

推荐答案

Use javascript interface. Combine javascript with java and make custom dialog in java At first, enable javascript

webview.getSettings().setJavaScriptEnabled(true);

On second, you need to add javascript interface

wv.addJavascriptInterface(new DemoJavaScriptInterface(), "js");

And third, load url;

webview.loadUrl("javascript:[javascript function name here]()");

Here is some code for you, hope this can help https://github.com/scottagarman/Android-JavaScript-Interface-Example

Code credit go to the developer called "scottagarman" .

其他推荐答案

You can do it his way:

public void onCreate(Bundle savedInstanceState){
  ....
  webView.getSettings().setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new MyJSInterface(), "myappname");
  webView.loadUrl("http://mysite.com/"); // or some local asset
  ....
}

public class MyJSInterface{

....
 public void popup(){

//  AlterDialog etc.

 }
....

}


HTML from your website:

window.onload = function(){
   window.myappname.popup();
}

I wish it would be useful for you...

By the way, be careful with 2.3 version it had a corresponding bug, just FYI))