Open URL in WebView instead of default Browser

前端 未结 6 1866
不思量自难忘°
不思量自难忘° 2020-12-30 07:19

I am creating simple Webview application with some links on textview and open those links in webview instead of default browser. My

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 07:56

    The most important part of @Emanuel Moecklin 's answer is to subclass URLSpan and create your own URLSpan which has onClick overrided.

    Inspired by his answer, instead of following fully his steps, I sticked to using Linkify. However, for some other reasons, I created my own Linkify class, with almost the same code as original Linkify.

    And then, inside applyLink() in Linkify, I replaced

    URLSpan span = new URLSpan(url);
    

    with

    InAppURLSpan span = new InAppURLSpan(url);
    

    Where the code for InAppURLSpan is:

    public static class InAppURLSpan extends URLSpan {
    
        public InAppURLSpan(String url) {
            super(url);
        }
    
        @Override
        public void onClick(View widget) {
            String url = getURL();
            Log.i("TNC_URL", url);
            Intent intent = new Intent(widget.getContext(), SingleWebViewActivity.class);
            intent.putExtra(Constants.INTENT_URL, url);
            widget.getContext().startActivity(intent);
        }
    }
    

    And it worked like a charm~

提交回复
热议问题