如何从全文中查找特定单词,并使其可通过TextView单击超链接?

时间:2018-09-24 10:07:40

标签: android hyperlink textview spannablestring

我正在使用textview并设置从json获取的文本,

“这是我们与我们联系的页面,请致电给我们”

我从服务器获取的上述文本显示在第3个recyclerview项上,现在我尝试的内容在任何列表项中,如果有“ contact”字样,我正在尝试使其成为超链接并希望使其成为超链接可点击的。但它不起作用。

 String fulltext=brandList.get(position).getFAQAnswerText();
        String match="contact";

        if (fulltext.contains(match)) {
            System.out.println("Keyword matched the string" );
             ss = new SpannableString(fulltext);

            ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        holder.tvans.setText(ss);
        holder.tvans.setMovementMethod(LinkMovementMethod.getInstance());

3 个答案:

答案 0 :(得分:3)

textView.setText("this is clickable text");
if (fulltext.contains("clickable")) {
    setClickableHighLightedText(textView, "clickable", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO: do your stuff here 
        }
    });
}

将此方法放在您的Util类中。

/**
 * use this method to set clickable highlighted a text in TextView
 *
 * @param tv              TextView or Edittext or Button or child of TextView class
 * @param textToHighlight Text to highlight
 */
public void setClickableHighLightedText(TextView tv, String textToHighlight, View.OnClickListener onClickListener) {
    String tvt = tv.getText().toString();
    int ofe = tvt.indexOf(textToHighlight, 0);
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            if (onClickListener != null) onClickListener.onClick(textView);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(tv.getContext().getResources().getColor(R.color.colorPrimary));
            ds.setUnderlineText(true);
        }
    };
    SpannableString wordToSpan = new SpannableString(tv.getText());
    for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
        ofe = tvt.indexOf(textToHighlight, ofs);
        if (ofe == -1)
            break;
        else {
            wordToSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

输出

image

更新

  • 更改跨度颜色

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(tv.getContext().getResources().getColor(R.color.spanColor));
        }
    
  • 更改突出显示/ onTouch颜色

    textView.setHighlightColor(getResources().getColor(R.color.onTouchColor));

答案 1 :(得分:0)

您可以使用SpannableString类,在“活动”或“实用程序”中使用“使用以下方法”来解决此问题

public static void makeLinks(TextView textView, String[] links, ClickableSpan[] clickableSpans) {
    SpannableString spannableString = new SpannableString(textView.getText());
    for (int i = 0; i < links.length; i++) {
        ClickableSpan clickableSpan = clickableSpans[i];
        String link = links[i];

        int startIndexOfLink = textView.getText().toString().indexOf(link);
        spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

您必须在这样的活动中初始化它

Utility.makeLinks(textViewInsance, new String[]{"Your text which need to highlight and click event",}, new ClickableSpan[]{new ClickableSpan() {
        @Override
        public void onClick(View widget) {
        }
    }
    });

答案 2 :(得分:0)

  • 您可以使用以下方法从字符串中找到特定单词:

    如果(fullText.toLowerCase()。indexOf(singleWord.toLowerCase())> -1){

    find = true;
    

    }

使用JAVA代码使文本超链接为:

    TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.kainaatsingha.com'> Kainaat Singha</a>";
textView.setText(Html.fromHtml(text));

或使用XML

android:autoLink="web"
android:linksClickable="true"