Android的 - 为什么web视图重定向到网页浏览器视图、重定向、网页浏览器、Android

2023-09-05 02:05:56 作者:時光走了很遠

我尝试一个简单的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.

请帮我。

推荐答案

您必须覆盖标准的行为,这将启动浏览器时,链接被点击。使用WebViewClient's shouldOverrideUrlLoading().有这样在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());

 
精彩推荐