【问题标题】:Multiple clickable link in textview文本视图中的多个可点击链接
【发布时间】:2015-07-31 11:21:10
【问题描述】:

我想像这样在 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(查询字符串)的不同链接后显示不同的 toast。

【问题讨论】:

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


【解决方案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】:
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());

【讨论】:

    【解决方案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);
            }
    

    【讨论】:

    • reporte.setMovementMethod(LinkMovementMethod.getInstance());在 setTextViewHTML(); 之后是必须的;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多