Android的 - 为什么web视图重定向到网页浏览器 [英] Android - why webview redirect to webbrowser
问题描述
我尝试一个简单的web视图器件的应用。这是我的code:
i try a simple webView appication. and this is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://eepis-its.edu");
}
public class myWebClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
}
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()){
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
和,当我尝试点击该网站的超链接,我重定向到手机浏览器。为什么它的发生? 我想要的是,当我点击超链接的显示上的WebView了。
and, when i try to click hyperlink on that site, i redirected to mobile browser. why its happen? what i wanted is, when i click hyperlink its show on webview too.
请帮我。
推荐答案
您必须覆盖标准的行为,这将启动浏览器时,链接被点击。使用<一个href="http://developer.android.com/reference/android/webkit/WebViewClient.html"><$c$c>WebViewClient$c$c>'s <一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29"><$c$c>shouldOverrideUrlLoading()$c$c>.有这样在SDK的例子(复制下面的方便)。
You have to override the standard behavior, which launches the browser when links are clicked. Use a WebViewClient
's shouldOverrideUrlLoading()
. There is an example of this in the SDK (copied below for convenience).
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
...那么你只需要调用 someWebView.setWebViewClient(新HelloWebViewClient());
这篇关于Android的 - 为什么web视图重定向到网页浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!