8

我有一个 webview,我正在从 web 服务调用该 wv 中的数据,并且在 webview 的整个描述中,最后有一个链接。所以,我的问题是我想打开一个新的活动 onclick 该链接既不是 onclick of webview 也不是 ontouch of webview

4

1 回答 1

17

您需要为shouldOverrideUrlLoading. 您必须WebViewClient为您的 webview 设置一个,并且在此方法内部,您需要有一些逻辑来识别该链接,然后打开新的Activity. 就像是:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wv = (WebView) findViewById(R.id.myWebView);
        wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(isURLMatching(url)) {
                    openNextActivity();
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    protected boolean isURLMatching(String url) {
            // some logic to match the URL would be safe to have here
        return true;
    }

    protected void openNextActivity() {
        Intent intent = new Intent(this, MyNextActivity.class);
        startActivity(intent);
    }
于 2013-07-25T15:25:33.667 回答