1

我正在将一个 html 字符串显示到一个包含一些链接的文本视图中,我在下面的代码的帮助下显示它:

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

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

4

3 回答 3

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

    }
于 2013-11-12T06:50:54.067 回答
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;
        }
    });

请参阅此处的文档以在其中找到最合适的方法...:)

希望这可以帮助...

于 2013-11-11T13:22:39.647 回答
0

我要解决的方法是将textview的文本设置为url,然后单击使用

myWebView.loadUrl(tV.getText().toString());
于 2013-11-11T13:19:25.030 回答