不懂啊??!
private?void?init()?{ webView?=?(WebView)?findViewById(R.id.web); webView.loadUrl(url); /* webView.setWebViewClient(new?WebViewClient()?{ public?boolean?shouldOverrideUrlLoading(WebView?view,?String?url)?{ return?super.shouldOverrideUrlLoading(view,?url); } }); */ WebSettings?settings?=?webView.getSettings(); settings.setJavaScriptEnabled(true); }
init這么寫會使用瀏覽器,但把注釋去掉后就直接在WebView中加載了
為什么調用父類的函數會出現不同的結果??
2017-08-19
shouldOverrideUrlLoading在api上的描述如下:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Give the host application a chance to take over the control when a > new url is about to be loaded in the current WebView.?
If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url.?
If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
Parameters
view : The WebView that is initiating the callback.
url ?: The url to be loaded.
Returns True if the host application wants to leave the current WebView and handle the url itself, otherwise return false. ? ?
翻譯一下,三種情況:
若沒有設置?WebViewClient?則在點擊鏈接之后由系統處理該 url(通常是將點擊事件作為intent對象發給系統),系統使用瀏覽器打開或彈出瀏覽器選擇對話框。
若設置?WebViewClient?且該方法返回?true?,則說明由應用的代碼處理該 url,WebView?不處理。
若設置?WebViewClient?且該方法返回?false,則說明由?WebView?處理該 url,即用?WebView?加載該 url。
很明顯,注釋后由系統處理,系統調用了瀏覽器,取消注釋調用父類方法時,父類方法的源碼如下:
@Override
public?boolean?shouldOverrideUrlLoading(WebView?view,?String?url)?{
mRedirectCount++;
mRedirectUrl?=?url;
view.loadUrl(url);
return?true;
}
最后返回的是true,即不跳轉到外部瀏覽器