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

Jav*_*oid 6 android android-webview

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

gun*_*nar 13

您需要提供实现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);
    }
Run Code Online (Sandbox Code Playgroud)