Web视图onclick事件,用于该webview中的特定链接

时间:2013-07-25 15:10:55

标签: android android-webview

我有一个webview,我从web服务调用该wv中的数据,在webview中的整个描述中,最后有一个链接。所以,我的问题是我想打开一个新的活动点击该链接既没有onview的webview也没有onview的webview

1 个答案:

答案 0 :(得分:12)

您需要为shouldOverrideUrlLoading提供实施。您必须为您的网页浏览设置WebViewClient,在此方法中,您需要有一些识别该链接的逻辑,然后打开新的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);
    }