问题描述
当我在我的 web 视图中按下 ebay.com 上带有此代码的按钮时:
When I press a button with this code on ebay.com in my webview:
按钮的html:
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"
如果在我的 webview 中按下此按钮,它会将新窗口从我的 webview 发送到 android 浏览器.但是如果在安卓浏览器中按下相同的按钮,它会弹出这个很酷的对话框类型的弹出窗口(见图)
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)
如果在我的 web 视图中按下此按钮,我想像浏览器一样在 eBay 上打开弹出式窗口.以便用户在完成弹出窗口后将其关闭以将其返回到我的应用程序.
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.
这可能吗?
这是我目前所拥有的:
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 上的按钮启动):
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):
推荐答案
你可以按照他的方式去做:
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();
}
希望对你有用……
顺便说一句,小心 2.3 版本它有一个对应的 错误,仅供参考))
By the way, be careful with 2.3 version it had a corresponding bug, just FYI))
这篇关于可能的 WebView OnCreateWindow 制作弹出窗口(对话框样式)?像安卓浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!