【问题标题】:How to put android WebView in AlertDialog?如何将 android WebView 放入 AlertDialog?
【发布时间】:2019-12-24 14:25:02
【问题描述】:

我正在尝试将 webview 放入 alertdialog,我正在使用我在堆栈中某处找到的代码,但由于某种原因,它不起作用。

这是我的代码示例:

holder.textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Google:");

                View view = LayoutInflater.from(context).inflate(R.layout.webLayout, null);

                WebView wv = view.findViewById(R.id.webview);

                wv.loadUrl(link);
                wv.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView v, String url) {
                        v.loadUrl(url);
                        return true;
                    }
                });

                alert.setView(view);
                alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
                alert.create().show();
            }
        });

webLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</LinearLayout>

点击按钮后只显示空对话框:

【问题讨论】:

  • for some reason, it doesn't work 你必须告诉我们你的意思。 什么不起作用
  • 点击按钮后只显示空对话框。
  • 这能回答你的问题吗? android: webview inside dialog or popup

标签: android


【解决方案1】:

您是否已将 INTERNET 权限添加到清单中? &lt;uses-permission android:name="android.permission.INTERNET"/&gt;

你必须先创建一个 xml 布局,然后把 webview 放到布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Configure the webview as you want-->
    <WebView
        android:id="+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</LinearLayout>

创建一个视图对象而不是一个 webview

View view = LayoutInflater.from(context).inflate(R.layout.layout_with_webview , null)

现在创建一个 webview 对象使用WebView webView = view.findViewById(R.id.webview) 将该视图传递给alert.setView(view)

还有最重要的alertDialogBuilder.show()

【讨论】:

  • show() 自动调用 create()。你不需要自己创造
  • 你确定吗?
  • 是的,我还要说,create() 不是必需的。
  • @daniel.jbatiz 更新了第一篇文章,仍然只是空对话框显示
  • 您是否添加了互联网权限
【解决方案2】:

只需替换

alert.create().show();

进入

alert.show();

更新:

请在您的网址中使用 https:// 而不是 http:// 或空白。如果您真的想使用非安全 url,您还需要添加明文支持。请在此处查看如何配置(https://stackoverflow.com/a/50834600/1084174

【讨论】:

  • 确实不需要 create() 因为 show() 调用 create() ,但错误似乎出在他试图加载的特定 url 上(google.com 只是为了检查如果有效
  • 在这种情况下,问题似乎出在防火墙上。可能是不允许使用海报网址。他需要检查其他网址。 @丹尼尔
  • 他尝试使用 google 和 facebook 并正常工作
【解决方案3】:

在 AndroidManifest.xml 中试试这个

  <application
        android:name="com.****.***"
        ****************
        ************
        *******
        .
        .
        .
        android:usesCleartextTraffic="true"
        >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多