WebView 未在 AlerDialog 中显示

发布于 2024-12-06 06:46:18 字数 3045 浏览 1 评论 0原文

我正在尝试在“AlertDialog”中显示“WebView”。 为此,我提到了此处发布的问题之一:在 AlertDialog 中显示 WebView 它成功打开了对话框窗口。但我不知道它为什么不显示网页内容。 这是我的“print_webview”文件,我在 AlertDialog 窗口中膨胀该文件:

'<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/root">
  <WebView
  android:id="@+id/dialog_webview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>    
</LinearLayout>'

这是我的 Java 文件:

'public class CloudPrintTest extends Activity {
    static final int GOOGLE_CLOUD_PRINT_DIALOG = 1; //dialog ID for google cloud print

    /** Called when the activity is first created. */
    Button printButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        printButton = (Button)findViewById(R.id.button1);
        printButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //startActivity(new Intent(CloudPrintTest.this,PrintDialog.class));
                showDialog(GOOGLE_CLOUD_PRINT_DIALOG);
            }
        });
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
        case GOOGLE_CLOUD_PRINT_DIALOG:
            //LayoutInflater layoutInflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //final View dialogLayout = layoutInflator.inflate(R.layout.print_webview, null);
            LayoutInflater inflater = LayoutInflater.from(CloudPrintTest.this);
            View alertDialogView = inflater.inflate(R.layout.print_webview, null);
            WebView myWebView = (WebView)alertDialogView.findViewById(R.id.dialog_webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            myWebView.setWebViewClient(new PrintTestWebViewClient());
            myWebView.loadUrl("http://www.google.com/");
            AlertDialog.Builder builder = new AlertDialog.Builder(CloudPrintTest.this);
            builder.setView(alertDialogView);
            builder.setTitle("Google Cloud Print");
            builder.setCancelable(true);
            AlertDialog printDialog = builder.create();
            return printDialog;
        }
        return null;
    }

    private class PrintTestWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }   
    }
}'

我已经尝试了所有可能的方法,但没有得到结果。 请帮忙。 问候

I am trying to display 'WebView' in 'AlertDialog'.
For that I referred one of the question posted here: Displaying WebView in AlertDialog
It successfully opens the dialog window. But I dont know some how its not showing the web content.
This is my 'print_webview' file, which I am inflating in AlertDialog window:

'<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/root">
  <WebView
  android:id="@+id/dialog_webview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>    
</LinearLayout>'

This is my Java file:

'public class CloudPrintTest extends Activity {
    static final int GOOGLE_CLOUD_PRINT_DIALOG = 1; //dialog ID for google cloud print

    /** Called when the activity is first created. */
    Button printButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        printButton = (Button)findViewById(R.id.button1);
        printButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //startActivity(new Intent(CloudPrintTest.this,PrintDialog.class));
                showDialog(GOOGLE_CLOUD_PRINT_DIALOG);
            }
        });
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
        case GOOGLE_CLOUD_PRINT_DIALOG:
            //LayoutInflater layoutInflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //final View dialogLayout = layoutInflator.inflate(R.layout.print_webview, null);
            LayoutInflater inflater = LayoutInflater.from(CloudPrintTest.this);
            View alertDialogView = inflater.inflate(R.layout.print_webview, null);
            WebView myWebView = (WebView)alertDialogView.findViewById(R.id.dialog_webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            myWebView.setWebViewClient(new PrintTestWebViewClient());
            myWebView.loadUrl("http://www.google.com/");
            AlertDialog.Builder builder = new AlertDialog.Builder(CloudPrintTest.this);
            builder.setView(alertDialogView);
            builder.setTitle("Google Cloud Print");
            builder.setCancelable(true);
            AlertDialog printDialog = builder.create();
            return printDialog;
        }
        return null;
    }

    private class PrintTestWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }   
    }
}'

I have tried all the possible ways, but didnt get the result.
Please help.
Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-12-13 06:46:19

我建议你创建一个活动。

当您在清单中注册它时。给它一个对话框外观

<activity android:theme="@android:style/Theme.Dialog">

What i would suggest is you creating an activity.

And when you register it in your manifest. Give it the Dialog look with

<activity android:theme="@android:style/Theme.Dialog">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文