如何在 WebView 上读取点击的 link 的 url 而无需新建 url?

How to read clicked link's url on WebView without going new url?

我有一个Webiew。我想阅读已点击 link 的 url 但 Webiew 不能阅读此 url。 WebView 必须保持同一页面。

示例:用户点击 "https://whosebug.com/questions" button and app read this link. But WebView must still stay on "https://whosebug.com/" 没有刷新。 我可以阅读点击的 link 的 URL,但我无法停止 WebView,它会转到新的 url。

@Sudheesh suggest you should extend WebView provide an new implementation of shouldOverrideUrlLoading。 与 Web View onclick event for a specific link in that webview .

的情况相同

我认为您应该避免调用 super.shouldOverrideUrlLoading(view, url); 以防止默认行为。

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);
                return false;
            }
        });
    }

    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);
    }