2012-09-23 90 views
2

我想構建一個沒有PhoneGap的Android webApplication,並且我有一個問題:當我點擊一個鏈接時,它會打開android默認瀏覽器......所以如何加載webview在同一個webview Android中的鏈接?如何在同一個webview中加載webview鏈接Android

這裏是我的代碼:

package com.example.mobilewebview; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.webkit.WebView; 
//import android.webkit.WebViewClient; 
import android.webkit.WebSettings; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     WebView myWebView = (WebView) findViewById(R.id.webview); 
     WebSettings webSettings = myWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     myWebView.loadUrl("http://m.google.com"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

謝謝!

回答

4

您必須攔截您的WebView可能用來執行自定義實施的URL鏈接。

您必須爲WebView創建一個WebViewClient,然後重寫public boolean shouldOverrideUrlLoading(WebView view, String url)方法來實現此目的。

示例代碼:

//web view client implementation 
private class CustomWebViewClient extends WebViewClient { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     //do whatever you want with the url that is clicked inside the webview. 
     //for example tell the webview to load that url. 
     view.loadUrl(url); 
     //return true if this method handled the link event 
     //or false otherwise 
     return true; 
    } 
}} 

//in initialization of the webview: 
.... 
webview.setWebViewClient(new CustomWebViewClient()); 
.... 

告訴我,如果沒有什麼幫助。

相關問題