从android中的html字符串单击时,如何获取href链接打开到webView?

时间:2013-11-11 13:15:04

标签: android webview

我在一个包含一些链接的textview中显示一个html字符串,我在下面的代码的帮助下将其展开:

itemContent.setText(Html.fromHtml(jo.getString("itemContent")));
                            itemContent.setMovementMethod(LinkMovementMethod.getInstance());

点击该链接将我引导至网络浏览器并成功打开该链接,但我想将该链接打开到我的应用程序的webView中。我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

呀!最后我完成了我想要的东西。我使用webView客户端来实现这一目标。以下是我传递包含url的html字符串的方法:

@SuppressLint("SetJavaScriptEnabled")
    private void loadWebView(WebView webView,String htmlString)
    {
     try {

             final String mimeType = "text/html";
             final String encoding = "UTF-8";


                WebViewClient yourWebClient = new WebViewClient()
                {
                    // Override page so it's load on my view only
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView  view, String  url)
                    {
                     // This line we let me load only pages inside  Webpage
                     if ( url.contains("") == true )
                        // Load new URL Don't override URL Link
                        return false;

                     // Return true to override url loading (In this case do nothing).
                     return true;
                    }
                };


                // Get Web view
                webView.getSettings().setJavaScriptEnabled(true);   
                webView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
                                                       //if ROM supports Multi-Touch      
                webView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
                webView.setWebViewClient(yourWebClient);

                // Load URL
                webView.loadDataWithBaseURL("", htmlString, mimeType, encoding, "");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

答案 1 :(得分:1)

不要使用默认的LinkMovementMethod,而是像......一样自己实现

itemContent.setMovementMethod(new MovementMethod() {

        @Override
        public boolean onTrackballEvent(TextView widget, Spannable text,
                MotionEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public boolean onTouchEvent(TextView widget, Spannable text,
                MotionEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public void onTakeFocus(TextView widget, Spannable text, int direction) {
            // TODO: Do here what you want

        }

        @Override
        public boolean onKeyUp(TextView widget, Spannable text, int keyCode,
                KeyEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public boolean onKeyDown(TextView widget, Spannable text, int keyCode,
                KeyEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public boolean onGenericMotionEvent(TextView widget, Spannable text,
                MotionEvent event) {
            // TODO: Do here what you want
            return false;
        }

        @Override
        public void initialize(TextView widget, Spannable text) {
            // TODO: Do here what you want

        }

        @Override
        public boolean canSelectArbitrarily() {
            // TODO: Do here what you want
            return false;
        }
    });

请参阅文档HERE,找到最合适的方法......:)

希望这会有所帮助......

答案 2 :(得分:0)

我的方法就是将textview的文本设置为url,然后点击使用

myWebView.loadUrl(tV.getText().toString());