2011-05-31 48 views

回答

4

我想你想顯示其包含來自網頁視圖網頁視圖應用程序中的彈出窗口。

需要兩個步驟。首先,你需要重寫onJSAlert()方法在WebChromeClient類,以使在網頁視圖彈出窗口:

public class MyWebChromeClient extends WebChromeClient { 
     @Override 
     public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) { 
      final JsResult finalJsResult = jsResult; 
      new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finalJsResult.confirm(); 
       } 
      }).setCancelable(false).create().show(); 
      return true; 
     } 
    } 

,並添加到您的WebView:

MyWebChromeClient myWebChromeClient = new MyWebChromeClient(); 
webView.setWebChromeClient(myWebChromeClient); 

然後,您可以添加自定義網頁流量您AlertDialog(以取代AlertDialog以上):

public OnClickListener imageButtonViewOnClickListener = new OnClickListener() { 
     public void onClick(View v) { 

      LayoutInflater inflater = LayoutInflater.from(MyActivity.this);    
      View alertDialogView = inflater.inflate(R.layout.alert_dialog_layout, null); 
      WebView myWebView = (WebView) findViewById(R.id.DialogWebView); 
      myWebView.loadData(webContent, "text/html", "utf-8"); 
      AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
      builder.setView(alertDialogView); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }).show(); 
     } 
    }; 

的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="vertical"> 
<webView android:id="@+id/DialogWebView" android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:layout_marginLeft="20dip" 
    android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" /> 
+0

您是否知道我爲什麼會在 處出錯並顯示錯誤myWebView.loadData(webContent,「text/html」,「utf-8」); webContent無法解析? – Colby 2011-06-01 01:26:31

+0

@Colby我想建議你先嚐試一個最簡單的webContent。像「myWebView.loadData(」

web content

「,」text/ html「,」utf-8「);」。希望這可以幫助。 – ThinkChris 2011-06-01 02:04:13

+1

@Colby Additionaly,你可能想知道loaddata()的限制,可以在這裏看到:http://groups.google.com/group/android-developers/browse_thread/thread/d7bf4187b731c6ae?pli=1 – ThinkChris 2011-06-01 02:07:07