如何在android中的popupwindow中打开webview?

时间:2017-02-08 06:12:34

标签: android android-webview popupwindow

我想在我的android应用程序中添加PopupWindow中的Web视图。这只适用于警告对话框。 这是我的代码:

 AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("Google");
            WebView wv = new WebView(MainActivity.this);
            wv.loadUrl("http:\\www.google.com");
            wv.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);

                    return true;
                }
            });

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

但我想用弹出窗口来做这件事。

4 个答案:

答案 0 :(得分:0)

FrameworkKeys.BONUS_LEVEL_25

这是layout.main

textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.main, null);
            WebView wv = (WebView) dialogView.findViewById(R.id.web);
            wv.loadUrl("https://www.google.com");
            wv.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);

                    return true;
                }
            });

            dialogBuilder.setView(dialogView);
            Dialog markerPopUpDialog = dialogBuilder.create();
            markerPopUpDialog.show();
        }
    });

答案 1 :(得分:0)

您也可以通过使用活动来完成此操作。首先,您需要使用Web视图进行活动,并使用以下代码在java文件中设置布局宽度和高度....

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int) (width*0.9),(int)(height*0.8));

并在Android Manifest文件中选择主题" @ style / AppTheme.CustomThemeForWebViewPOPUp"对于此活动和样式文件中放置此代码....

<style name="AppTheme.CustomThemeForWebViewPOPUp">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowBackground">@drawable/popup_activity_design</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

答案 2 :(得分:0)

根据您的要求,您应该使用自定义视图自定义警报对话框。

制作一个xml文件:

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

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

    <ProgressBar
        android:id="@+id/prg"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerInParent="true" />

</RelativeLayout>

<Button
    android:id="@+id/button3"
    style="@android:style/Widget.Holo.Button.Borderless"
    android:layout_width="wrap_content"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:layout_gravity="center|right"
    android:text="Close" />

</LinearLayout>

现在在java文件中实现代码:

 final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setTitle("Google");
    View view = getLayoutInflater().inflate(R.layout.file_name, null);
    alert.setView(view);

    WebView wv = (WebView) view.findViewById(R.id.webview);
    pb = (ProgressBar) view.findViewById(R.id.prg);
    Button btnClose = (Button) view.findViewById(R.id.button3);
    pb.setVisibility(View.VISIBLE);

    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });
    wv.setWebViewClient(new MyWebViewClient());
    wv.loadUrl("http://www.google.com");

    alertDialog = alert.create();
    alertDialog.show();
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(alertDialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    alertDialog.getWindow().setAttributes(lp);

还可以自定义WebViewClient

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        if (pb.getVisibility() == View.GONE) {
            pb.setVisibility(View.VISIBLE);
        }

        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        System.out.println("on finish");
        if (pb.getVisibility() == View.VISIBLE) {
            pb.setVisibility(View.GONE);
        }
    }
}

注意:alertDialog个对象声明为全局。

答案 3 :(得分:0)

最后,我做到了。如果有人需要这样做,这是我的代码。

            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("Terms and conditions");
            View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_termsdialog,null);
            WebView wv = (WebView) view.findViewById(R.id.web);
            progressbarterms= (ProgressBar) view.findViewById(R.id.progressbarterms);
            wv.loadUrl("my url");
            WebSettings webSettings = wv.getSettings();
            webSettings.setJavaScriptEnabled(true);
            wv.setWebViewClient(new TermsClient());
            alert.setView(view);
            alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            alert.show();
        }
class TermsClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
                    progressbarterms.setVisibility(View.VISIBLE);
        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

                    progressbarterms.setVisibility(View.GONE);
    }
}

布局文件包含webview和进度条。