TextView中的HTML文本并在新活动中打开链接

时间:2018-08-02 15:24:15

标签: java android kotlin

我需要在启用链接单击的TextView中显示此HTML文本。

mention test dpubek 
<span class="proflinkWrapper">
<span class="proflinkPrefix">+</span>
<a class="proflink" href="https://plus.google.com/103010988161443919979" oid="103010988161443919979">movietrailer moviedb</a>
</span> <span class="proflinkWrapper">
<span class="proflinkPrefix">+</span>
<a class="proflink" href="https://plus.google.com/103675306250275917544" oid="103675306250275917544">Movieweb</a>
</span> <span class="proflinkWrapper">
<span class="proflinkPrefix">+</span>
<a class="proflink" href="https://plus.google.com/114941226520632356413" oid="114941226520632356413">Luba Tesler</a></span>

mention test dpubek <span class="proflinkWrapper"><span class="proflinkPrefix">+</span><a class="proflink" href="https://plus.google.com/103010988161443919979" oid="103010988161443919979">movietrailer moviedb</a></span> <span class="proflinkWrapper"><span class="proflinkPrefix">+</span><a class="proflink" href="https://plus.google.com/103675306250275917544" oid="103675306250275917544">Movieweb</a></span> <span class="proflinkWrapper"><span class="proflinkPrefix">+</span><a class="proflink" href="https://plus.google.com/114941226520632356413" oid="114941226520632356413">Luba Tesler</a></span>

使用此代码可以轻松完成此操作

val span = Html.fromHtml(post.content)

问题是,我需要使链接可单击,但不应在浏览器中打开它,而是需要将该链接传递给我的android应用程序中的下一个活动。希望我足够清楚!

2 个答案:

答案 0 :(得分:0)

这样使用,希望对您有帮助

private void customTextView(TextView view) {
    SpannableStringBuilder spanTxt = new SpannableStringBuilder(
            "By continuing, I agree to");
    spanTxt.append("Terms & Conditions");
    spanTxt.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Intent intent = new Intent(mContext, TermsAndConditionActivity.class);
            intent.putExtra("URL", UserModel.getInstance().termsUrl);
            intent.putExtra("title", "Terms & Conditions");
            intent.putExtra("requestFor", 1);
            startActivity(intent);

        }
    }, spanTxt.length() - "Terms & Conditions".length(), spanTxt.length(), 0);
    spanTxt.append(" and ");
    spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 38, spanTxt.length(), 0);
    spanTxt.append("Privacy Policy");
    spanTxt.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Intent intent = new Intent(mContext, TermsAndConditionActivity.class);
            intent.putExtra("URL", UserModel.getInstance().privacyUrl);
            intent.putExtra("title", "Privacy Policy");
            intent.putExtra("requestFor", 2);
            startActivity(intent);

        }
    }, spanTxt.length() - "Privacy Policy".length(), spanTxt.length(), 0);
    spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 54, spanTxt.length(), 0);
    view.setMovementMethod(LinkMovementMethod.getInstance());
    view.setText(spanTxt, TextView.BufferType.SPANNABLE);
}

答案 1 :(得分:0)

创建一个新类 CustomMovementMethod ,扩展如下所示的 LinkMovementMethod ,然后覆盖 onTouchEvent(..)方法

public class CustomMovementMethod extends LinkMovementMethod {
    private static CustomMovementMethod sInstance;


    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            URLSpan[] links = buffer.getSpans(off, off, URLSpan.class);


            if (links.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    Intent intent=new Intent(widget.getContext(),HandleUrlActivity.class);
                    intent.putExtra("url",links[0].getURL());
                    widget.getContext().startActivity(intent);

                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer,
                            buffer.getSpanStart(links[0]),
                            buffer.getSpanEnd(links[0]));
                }
                return true;
            } else {
                Selection.removeSelection(buffer);
            }
        }

        return super.onTouchEvent(widget, buffer, event);
    }



    public static MovementMethod getInstance(){
        if (sInstance == null)
            sInstance = new CustomMovementMethod();

        return sInstance;
    }

然后在设置文本后设置移动方法,

示例代码:

        final String htmlString="<span class=\"proflinkWrapper\">\n" +
                "<span class=\"proflinkPrefix\">+</span>\n" +
                "<a class=\"proflink\" href=\"https://plus.google.com/103010988161443919979\" oid=\"103010988161443919979\">movietrailer moviedb</a>\n" +
                "</span> <span class=\"proflinkWrapper\">\n" +
                "<span class=\"proflinkPrefix\">+</span>\n" +
                "<a class=\"proflink\" href=\"https://plus.google.com/103675306250275917544\" oid=\"103675306250275917544\">Movieweb</a>\n" +
                "</span> <span class=\"proflinkWrapper\">\n" +
                "<span class=\"proflinkPrefix\">+</span>\n" +
                "<a class=\"proflink\" href=\"https://plus.google.com/114941226520632356413\" oid=\"114941226520632356413\">Luba Tesler</a></span>";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mText.setText(Html.fromHtml(htmlString,Html.FROM_HTML_MODE_LEGACY));
        }else{
            mText.setText(Html.fromHtml(htmlString));
        }
        mText.setMovementMethod(CustomMovementMethod.getInstance());

然后在下一个活动中使用

  

getIntent()。getStringExtra(“ url”)

点击该网址。