如何从全文中找到特定的单词并使其超链接可通过文本视图点击?
How to find particular word from full text and make it hyperlink clickable with textview?
我正在使用 textview 并设置从 json、
获得的文本
"This is our contact us page please call us"
上面的文本是我从服务器获取的,它显示在第 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());
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 class。
/**
* 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());
}
}
}
输出
更新
更改跨度颜色
@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));
您可以使用 SpannableString Class 解决此问题,在 Activity 或 Utility
中使用 Below 方法
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);
}
并且您必须像这样在 activity 中初始化它
Utility.makeLinks(textViewInsance, new String[]{"Your text which need to highlight and click event",}, new ClickableSpan[]{new ClickableSpan() {
@Override
public void onClick(View widget) {
}
}
});
您可以使用以下方法从字符串中找到特定的单词:
if (fullText.toLowerCase().indexOf(singleWord.toLowerCase()) > -1) {
find = true;
}
使用JAVA代码制作文本heyperlink是:
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"
我正在使用 textview 并设置从 json、
获得的文本"This is our contact us page please call us"
上面的文本是我从服务器获取的,它显示在第 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());
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 class。
/**
* 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());
}
}
}
输出
更新
更改跨度颜色
@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));
您可以使用 SpannableString Class 解决此问题,在 Activity 或 Utility
中使用 Below 方法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);
}
并且您必须像这样在 activity 中初始化它
Utility.makeLinks(textViewInsance, new String[]{"Your text which need to highlight and click event",}, new ClickableSpan[]{new ClickableSpan() {
@Override
public void onClick(View widget) {
}
}
});
您可以使用以下方法从字符串中找到特定的单词:
if (fullText.toLowerCase().indexOf(singleWord.toLowerCase()) > -1) {
find = true;
}
使用JAVA代码制作文本heyperlink是:
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"