2011-03-31 93 views
1

我有一個鏈接,例如「http://google.com」,我需要在我的應用程序加載網頁或者在網絡視圖或其他一些視圖,但不是在android系統的默認瀏覽器,無論是可能與否。在android中的webview中加載網頁?

感謝

回答

1

我想你想申請內的WebView 負荷URL。如果我沒看錯的話,你可以有:

WebView browser = (WebView)findViewById(R.id.browser); 
browser.setWebViewClient(new WebViewClient() { 
    /* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link, 
    * but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     /* intercept all page load attempts and load yahoo.com instead */ 
     String myAlternativeURL = "http://yahoo.com"; 
     if (!url.equals(myAlternativeURL)) { 
      view.loadUrl(myAlternativeURL); 
      return true; 
     } 

     return false; 
    } 
}); 

browser.loadUrl("http://google.com"); 

並在一般情況下,你也可以使用webview.loadURL(URL)方法加載URL的網頁流量。

0

您需要延長WebViewClient和內推出的URL。

public class WebActivity extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     webview = (WebView) findViewById(R.id.wv); 
     webview.setWebViewClient(new WebC()); 
     webview.loadUrl("http://google.com"); 
    } 

    public class WebC extends WebViewClient 
    { 
     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
     { 
      super.onReceivedError(view, errorCode, description, failingUrl); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
    ... etc. 

而在你的佈局XML,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

    <WebView 
     android:id="@+id/wv" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
</LinearLayout>