我收到消息,但其中一些看起来像这样:
http://x.x.x.x:8080/DocumentViewer/viewer.html?.......
我想"过滤"这些消息转换为:"媒体消息。点击打开",隐藏网址。但是我需要让这个文字可以点击,并打开"隐藏"网址。
我发现我可以使用该属性&#34; autoLink = web&#34;在TextView中,设置为网络:<a href="link">TEXT</a>
但它不起作用,或者我做错了。
我所拥有的是:
if (mess.getBodyMessage().contains("DocumentViewer")) {
StringBuilder sb = new StringBuilder();
sb.append("<a href=\"" + mess.getBodyMessage() + "\">" + "Media Message.Click to open" + "</a>");
mess.setBodyMessage(sb.toString());
}
然后在视图中:
<xx.xx.xx.view.text.FontTextView
android:id="@+id/message_scout_text_text_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:autoLink="web"
android:gravity="left"
android:textColor="@color/text_navy"
android:textSize="14sp"
app:font="museo_sans_500.otf"
tools:text="test"/>
但正如我所说,不能工作
我怎样才能实现它?
答案 0 :(得分:0)
使用SpannableString
尝试TextView myTextView = new TextView(this);
String myString = "Some text [clickable]";
int i1 = myString.indexOf("[");
int i2 = myString.indexOf("]");
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setText(myString, BufferType.SPANNABLE);
Spannable mySpannable = (Spannable)myTextView.getText();
ClickableSpan myClickableSpan = new ClickableSpan()
{
@Override
public void onClick(View widget) { /* do something */ }
};
mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
答案 1 :(得分:0)
使用此方法
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
覆盖类
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}