在当前webview中打开弹出/外部站点链接

时间:2011-12-27 17:22:09

标签: android twitter hyperlink webview popup

我目前正在编写一个webview,它首先加载一个推特页面(比方说, NHL,http://twitter.com/nhl) 正如你所看到的,你可以找到NHL的推文,每个NHL的推文都有 用户点击的另一个链接,例如bit.ly/ujcNZo

在我的网页浏览中,如果我点击该链接(即bit.ly/ujcNZo),我的网页浏览, 1秒后,除了白色的推特图标外,不会显示任何内容 颜色背景,它应该加载内容,但它没有。

经过一段时间的调查后,我认为这与事实有关 推文中的链接(即bit.ly/ujcNZo)实际打开了 链接在一个单独的窗口(弹出?)而不是当前页面 链接已发布,我通过访问NHL的Twitter页面进行了验证 浏览器在我的笔记本电脑中。

我的问题是, 1.有没有办法可以加载外部链接的内容(bit.ly/ 例如ujcNZo在我当前的webview中?

1 个答案:

答案 0 :(得分:2)

您可以通过WebViewClient类来控制它。只需扩展它并覆盖默认实现以获得所需的配置,然后将其设置为当前webview的客户端。

<强>活动

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // set the webViewClient to a new instance of your custom WebViewClient

    webview.setWebViewClient(new WebActivityClient( this ));

}

自定义客户端

/** WebViewClient class for WebView in WebActivity */
private class WebActivityClient extends WebViewClient {

    public static final String TAG = "WebActivityClient";
    private Context context;

    /** Constructor used to grab the context */
    public WebActivityClient( Context context ) {
        this.context = context;
    }

    /** Override to load every link within the page inside this webview instead of using
     * android's default application
     * #7 http://developer.android.com/resources/tutorials/views/hello-webview.html */
    @Override
    public boolean shouldOverrideUrlLoading( WebView view, String url ) {
        view.loadUrl(url);
        return true;
    }

}

希望这有帮助!