如何在TextView Link中打开WebView中的URL?

时间:2017-01-06 06:17:59

标签: android android-webview

我收到Web Data并且我正在textView上设置它。嗯网络数据,有网址。我想点击给定的url,它应该在ApplicationWebView内打开。但它的发布new browser。请指导我。我所做的就是这里,

private void setTextViewHTML(TextView type_1_bodyTxt, String main_hrml) {
    CharSequence sequence = Html.fromHtml(main_hrml);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }

    if (main_hrml.contains("<p>")){
        if (!main_hrml.contains("<ul")) {
            GetDeviceResolution getDeviceResolution = new GetDeviceResolution((Demo) context);
            type_1_bodyTxt.setPadding(0, 0, 0, -(int) (getDeviceResolution.setHeight(0.06)));
        }else{
            GetDeviceResolution getDeviceResolution = new GetDeviceResolution((Demo) context);
            type_1_bodyTxt.setPadding(0, 0, 0, -(int) (getDeviceResolution.setHeight(0.02)));
        }
    }
    type_1_bodyTxt.setText(strBuilder);
    type_1_bodyTxt.setLinkTextColor(Color.RED);


    removeLine(type_1_bodyTxt, main_hrml);
    type_1_bodyTxt.setMovementMethod(LinkMovementMethod.getInstance());
}

private void removeLine(TextView type_1_bodyTxt, String content){
    Spannable s = (Spannable) Html.fromHtml(content);
    for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
        s.setSpan(new UnderlineSpan() {
            public void updateDrawState(TextPaint tp) {
                tp.setUnderlineText(false);
            }
        }, s.getSpanStart(u), s.getSpanEnd(u), 0);
    }

    type_1_bodyTxt.setText(s);
}



private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
            Intent intent = new Intent(context, WebViewActivity.class);
            intent.putExtra("url", span.getURL());
            context.startActivity(intent);
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

**我使用Intent传递url的WebActivity是**

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.astrolabe_web_link);

    loadUrl = getIntent().getStringExtra("url");
    WebView webview = (WebView) findViewById(R.id.webView);
    ImageView webClose = (ImageView) findViewById(R.id.webClose);
    loadUrl = getIntent().getStringExtra("url");
    if (loadUrl == null) {
    }

    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDefaultTextEncodingName("utf-8");
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url); // return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    });
    webClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
    webview.loadUrl(loadUrl);
}

}

1 个答案:

答案 0 :(得分:0)

当您点击带有链接的textview时,您必须打开具有webview的活动并加载您在textview上的URL。

webView.loadURL(textViewValue);
webView.setWebViewClient(new WebViewClient());

WebViewClient将在您的应用程序webview中打开URL,不会打开外部浏览器