textview中的多个可点击链接

时间:2015-05-19 14:43:13

标签: android html textview android-webview android-toast

我想在textview中设置html文本,如下所示:

<a href="?id=1>Toast id1</a>hello there <a href="?id=2>Toast id2</a>
hello there <a href="?id=3> Toast id3</a>

我希望在点击具有不同ID(查询字符串)的不同链接后显示不同的吐司。

3 个答案:

答案 0 :(得分:3)

String htmlText = "<body><h1>Heading Text</h1><p>This tutorial "
            + "explains how to display "
            + "<strong>HTML </strong>text in android text view.&nbsp;</p>"
            + "Example from <a href=\"www.ushatek.com\">"
            + "Ushatek<a>,<a href=\"www.google.com\">"
            + "Google<a>,<a href=\"Male\">"
            + "Male<a>,<a href=\"Female\">"
            + "Female<a></body>";

setTextViewHTML(textView, htmlText);

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

protected 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) {
                    Toast.makeText(getApplicationContext(), span.getURL(), Toast.LENGTH_LONG).show();
                }
            };
            strBuilder.setSpan(clickable, start, end, flags);
            strBuilder.removeSpan(span);
        }

答案 1 :(得分:1)

您可以使用android.text.style.ClickableSpan

 SpannableString ss = new SpannableString("Hello World");
    ClickableSpan span1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do some thing
        }
    };

    ClickableSpan span2 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do another thing
        }
    };

    ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 2 :(得分:0)

TextView textView= (TextView)view.findViewById(R.id.textViewAboutUs);

SpannableString ss = new SpannableString("Your String");

ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Toast.makeText(getActivity().getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();

        }
    };

    ss.setSpan(clickableSpan, starting_position, end_position, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //spanned string, for multiple string define multiple ss.setSpan

    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());