2012-02-07 113 views

回答

94

這裏是例子:

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
alert.setTitle("Title here"); 

WebView wv = new WebView(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(); 
+0

感謝它真正workding現在與alertdialog,爲什麼它現在使用popupwindow也? - user1194088剛纔 – 2012-02-07 08:58:55

+0

無法使用popupwindow? – 2012-02-07 09:05:07

+0

如果我想在頁面加載時顯示加載指示符,該怎麼辦? – user1940676 2013-11-24 10:11:23

4

你需要重寫setWebViewClient()..

mWebView = (WebView) view.findViewById(R.id.wv1); 
mWebView.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return false; 
    } 
}); 
mWebView.loadUrl(mUrl); 
0

PopupWindow代碼:

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
    try 
    { 
     int[] location = new int[2]; 

     (xml item where you want it to appear).getLocationOnScreen(location); 
     //Get x and y positions 
     p = new Point(); 
     p.x = location[0]; 
     p.y = location[1]; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
private void popTab(final Activity myActivity) 
{ 
    popupWidth = 350; 
    popupHeight = 600; 

    popup.setWidth(popupWidth); 
    popup.setHeight(popupHeight); 

    // Inflate the popup_layout.xml 
    LinearLayout viewGroup = (LinearLayout) myActivity.findViewById(R.id.myMainLayoutID); 
    LayoutInflater layoutInflater = (LayoutInflater) myActivity 
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layoutTab = layoutInflater.inflate(R.layout.mylayout, viewGroup); 

    //Get webview from xml 
    WebView wv = (WebView)layoutTab.findViewById(R.id.webView2); 

    // Creating the PopupWindow 
    final PopupWindow popup = new PopupWindow(layoutTab); 
     //Set to view 
    popup.setContentView(layoutTab); 

     //Setup webview 
    wv.loadUrl("http:\\www.google.com"); 
    wv.setWebViewClient(new WebViewClient() 
    { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      view.loadUrl(url); 

      return true; 
     } 
    }); 

    //Add some animation from style folder 
    popup.update(); 
    popup.setAnimationStyle(R.style.Animation); 
    popup.setFocusable(true); 

    popup.showAtLocation(layoutTab, Gravity.NO_GRAVITY, p.x, p.y); 
} 

使用popTab()在任何你喜歡。希望這有助於彈出窗口。

相關問題